Deploying NodeJS On Heroku

Heroku is a cloud platform that allows us to host our applications made on different programming platforms like NodeJS, Java,  Ruby, PHP, Python, .Net etc.
 
Today we are going to demonstrate how to deploy a nodejs application on Heroku. Heroku provides free account use which you can host maximum of 5 application, beyond which you need to purchase special account on it. The sample node js application that I would be using in this article is the one that I created in the blog. Please have a look at that as well. You need to install heroku ToolBelt in order to work on it on your system, Also you need to install GIT on your system because heroku works coupled with git.
 
Before starting, you must create a heroku account on the website.
 
Also, you need to add a file - Procfile to the root of your project. This file contains a one line code having information as to which file should the application start with. As in our case the file name is app.js, so the Procfile code is, 

web: node app.js 

So,now that we have our application handy, lets start deploying
 
Step 1 

Open cmd and reach to the directory location of your project. We need to make this folder as a git repository. In order to do this type the command: git init. This will turn the current folder location to a git repository.



Step 2
 

Now we use the command git add . This command is to add important information to all the files located at the current project directory.





Step 3

The next step is to write the changes in the files to the files in the created git repository. This is done by using the command git commit -m "new files". Please note that here "new files" is just the comment and has no more significance.



Step 4

This step is to login into the heroku account that you have created earlier. Just type the command heroku login and then provide the username as password as requested after that.

 


Step 5 

This step is now to create an application on heroku. Now you can either provide a name of the application from yourself or you can just leave it to heroku to decide a random name of the application and provide it to you.

In case you want to provide your own application name than use the command: heroku create nameofapp

If you want heroku to decide an app name for you than use the command: heroku create.



Step 6 

Now we are left with a final step to push our files to the server, this will be achieved by using the command: git push heroku master. This command will move all the changes to the server.





You can see in the sreenshot above that after the final deployment, you are provided with a url (highlighted), using which you can access your application.

Up Next
    Ebook Download
    View all
    Learn
    View all