Heroku

Another popular cloud-based hosting provider for Node.js apps is www.Heroku.com. The one thing that sets Heroku apart from other providers is the number of powerful add-ons that are available. Any kind of service you can imagine that your application will require is available as an add-on, including data stores, search, logging and analytics, email and SMS, workers and queuing, monitoring, and media. Each of these add-ons can be quickly and easily added to your service and integrated into your application.

Like Nodejitsu, Heroku allows you to register for a free account and work within the confines of their sandbox pricing plans. The plans are free, but limited in scope with regard to bandwidth, processing power, and so on. Most, if not all, of the add-ons typically also offer some sort of free sandbox or trial-based plan. Just like Nodejitsu, one of the add-ons we will be using with our Heroku app is MongoHQ, a cloud-based MongoDB service provider.

To get started, first go to http://heroku.com and sign up for your free account. While registration doesn't require a credit card, in order to include any add-ons with your application, you will have to have a credit card on file (even though you won't be charged unless you choose to scale up the services). After registering, click on the link in the confirmation email and provide a password; you will be presented with your Apps dashboard.

You'll note that the first thing you need to do is to download the Heroku Toolbelt (again, much like the jitsu CLI for Nodejitsu). Click on the Download button to download and install the Toolbelt. The Toolbelt is a CLI specifically used to create and deploy apps to Heroku, and gives you the heroku command.

Once you have the Toolbelt installed, open a command-line Terminal and change directories to your project's root. From there, execute the following command
to log in to Heroku:

    $ heroku login
    Enter your Heroku credentials.
    Email: [email protected]
    Password (typing will be hidden):
    Authentication successful.
  

Now that you're logged in, you can issue commands directly to your Heroku
account and use those commands to create an application, install add-ons, and deploy your project.

The first thing you'll want to do is to create a new application. Do so by executing heroku create from the command line:

    $ heroku create
    Creating secret-shore-2839... done, stack is cedar
    http://secret-shore-2839.herokuapp.com/ | [email protected]:secret-
shore-2839.git

After creating the app, Heroku randomly assigns it a unique name; in my case, secret-shore-2839 (don't worry, though, as this can easily be changed):

    $ heroku apps:rename imgploadr --app secret-shore-2839
    Renaming secret-shore-2839 to imgploadr... done
    http://imgploadr.herokuapp.com/ | [email protected]:imgploadr.git
    Don't forget to update your Git remotes on any local checkouts.
  

Let's address the last part next. Heroku relies on the Git source control on your machine in order to push your project source code up to your server, unlike Nodejitsu, which uses its own file transfer mechanism. Assuming that you followed the directions earlier with regard to Git and www.github.com, your project source code should be all set and committed to the master branch and ready to go. What we need to do next is to add a new remote for Git on your machine to point to Heroku; specifically, your new app.

Let's start with git init to initialize git in the current working directory and then execute the following command to create a new remote for Heroku:

    $ git remote add heroku [email protected]:imgploadr.git

Before you can push your source code up to your Heroku account, we need to take care of a few things.

A special file is required before your application will be able to run on your Heroku server. This file is called Procfile, and it specifically contains the command necessary to launch your application. Create this new file named Procfile (no extension) in the root of your project and include the following line:

    web: node server.js 

That's it! With that file, Heroku will use that command to launch your application. Now that you have Procfile set up and your project source code ready, there's only one thing left to do--install the MongoHQ add-on and configure your app to use it:

    $ heroku addons:create mongohq --app imgploadr
    Adding mongohq on imgploadr... done, v3 (free)
    Use 'heroku addons:docs mongohq' to view documentation.

With the MongoHQ add-on added, you can now configure the database itself and retrieve the connection string (much like you did earlier with Nodejitsu). Access your http://heroku.com Apps dashboard, and it should look something like the following screenshot:

The app's dashboard screen is a great place to get a snapshot of your application and a quick glance at its current cost. Since I'm using the sandbox and/or free plans for my application and add-ons, my current estimated monthly cost is $0.00. However, you can quickly and easily scale your apps, should you demand more power. Pay attention, as you can also just as quickly and easily escalate your monthly cost through the roof! (Scaling everything to maximum, I was able to get my estimated cost to roughly $60,000 per month!).

To configure your MongoHQ database, simply click on the MongoHQ link under the Add-ons section of your app's dashboard:

Click on the Admin tab with the gear icon below the Collections tab. Click on the Users tab and provide a username and password that your application will use to connect with your MongoHQ database. This will create the imgploadrdb username with a secure password. With the new user added, switch back to the Overview tab and copy the Mongo URI string.

Again, just like with Nodejitsu, edit the server.js file in your project and replace the mongoose.connect string with the new URI you just copied. Edit the string and replace <username> and <password> with the appropriate values, based on the new user account you just created. The server.jsmongoose.connect code should look as illustrated:

mongoose.connect('mongodb://imgploadrdb:[email protected]
m:10089/app26'); mongoose.connection.on('open', ()=>{ console.log('Mongoose connected.'); });

Since you just made changes to your project's source code, you need to remember to commit those changes to your Git repository master branch so that they can be uploaded to Heroku. Execute the following command to permanently commit these changes to your source code and upload your code to your Heroku server:

    $ git commit -am "Update mongoose connection string"
    $ git push heroku master
    Initializing repository, done.
    Counting objects: 50, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (43/43), done.
    Writing objects: 100% (50/50), 182.80 KiB | 0 bytes/s, done.
    Total 50 (delta 3), reused 0 (delta 0)
    ... npm install output ...
    To [email protected]:imgploadr.git
     * [new branch]      master -> master
  

The final step to get your application up and running is to create an instance of your server (basically, the equivalent of turning it on). To do this, execute the following command:

    $ heroku ps:scale web=1 --app imgploadr
    Scaling dynos... done, now running web at 1:1X.
    $ heroku open
    Opening imgploadr... done
  

Success! Hopefully, your browser launched and your website is up and running. Go ahead, give it a try and upload an image! Thanks to the bug we caught during the Nodejitsu deployment, this updated version of the application should work just fine.

While deploying with Heroku seems more complicated than Nodejitsu, this is probably because it uses Git source control to facilitate the transfer of your project files. Also, because Heroku is so flexible with the power of its scaling and add-ons, the Toolbelt CLI is a little more robust.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.22.179