Today, we are going to demonstrate how to deploy a Node.js application on Heroku. Heroku provides free account use, on which you can host a maximum of five applications. Beyond that, you need to purchase special account. The sample Node.js application that I will be using in this article is the one that I created here. Please have a look at that as well. You need to install Heroku ToolBelt in order for it 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 with information that helps decide which file the application starts with. As in our case, the file name is app.js, so the Procfile code is web: node app.js.
Let's Start Deploying
Open cmd and head for the directory location of your project. We need to make this folder a git repository. In order to do this, type the command:
git init. This will turn the current folder location to a git repository.
Now, we use the command
git add. This command is to add important information to all the files located at the current project directory:
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 a comment and has no more significance.
This step is to log into your Heroku account, which you created earlier. Just type the command
heroku login, then provide the username as password as requested after that:
Create an application on Heroku. You can either provide your name for the application, or you can just leave it to Heroku to decide a random name and give it to you.
In case you want to provide your own application name, use the command:
heroku create nameofapp. If you want Heroku to decide an app name for you, use the command:
Now we are left with a final step to push our files to the server. We'll do this by using the command:
git push heroku master. This command will move all the changes to the server.
You can see in the screenshot above that after the final deployment, you are provided with a URL (highlighted), which you can use to access your application.