Node.js app deployment in Heroku
Jul 24, 2012
Suresh Vasudev
4 minute read

What is Heroku

  1. It is a PaaS Provider
  2. It is designed around GIT workflow for deployment
  3. It is originally designed for hosting ROR application
  4. It now supports other type of applications like Node.js, Java, Python, Scala etc
  5. The basic service of Heroku is free

Node.js application deployment in Heroku

Create a basic Express Node.js application - herokudemo.

Make sure express module is globally installed. If not install it using the command :

npm install -g express
  1. express herokudemo
  2. cd herokudemo
  3. npm install
  4. node app.js

Check the application from http://localhost:3000

Create an account in Heroku

Step-1: Go to the URL - https://devcenter.heroku.com/articles/quickstart and follow the simple sign-up instruction.

If you want to use some additional services like MongoDb, then you need to verify your account using Credit card. However it will not be charged unless the base usage limit is crossed.

HK

Step-2: Install Heroku Toolbelt

It is a collection of application for managing and deploying application in Heroku. it consists of Heroku Client, which is a command-line tool for creating and managing Heroku applications. it also bundles GIT and Foreman (to execute the application locally)

Step-3: Open a Terminal (Command) window and login to Heroku account as follows

heroku login

System will prompt for user name and password. For the first time SSH public key will be generated and get registered against the account in Heroku.

HK

Now we are done with the Heroku setup and also the the base application which is to be deployed in Heroku

Preparing the application for Heroku Deployment

Now we need to prepare the application for Heroku Deployement as follows

Step-1: Check the app.js and see port is not hardcoded.

port MUST be defined either as a variable

var port = process.env.PORT || 3000;

In the Express Node.js it is already handled by default.

app.set('port', process.env.PORT || 3000);

Step-2: Make sure the port is not hardcoded in the listen method.

app.listen(port);

In the Express Node.js it is handled automatically as follows

listen(app.get('port')

Step-3: Create a file - Procfile in appliation root directory and add the folllowing line in it

web: node app.js

The application is now ready for Heroku deployment.

Adding the application to GIT

  1. git init .
  2. gitignore the node-modules directory

​ create a file .gitignore at root level and specify the node-modules directory in it.

  1. git add .
  2. git commit -m”Base Version”

The application is now checked in to GIT and it is ready for deployment in Heroku

Creating an application in Heroku

Let us create an application in Heroku using the following command

heroku create nodejsdemo --stack cedar

HK

Check the Heroku account to see the application created. As a part of this process we will get a git URL to the remote Heroku application

HK

Check the current status of the application : http://nodejsdemo.herokuapp.com/

HK

Now deploy the local application in Heroku using the git push command

git push heroku master

It will deploy the application in Heroku

HK

Now check again the application in Heroku :

HK

TroubleShooting during Deployment

If there is an issue with deployment, you will get the following error while accessing the application URL.

HK

To troubleshoot the deployment error check the Heroku logs

</p>
<p dir="ltr">heroku logs -t --app {APP_NAME}</p>
<p dir="ltr"> Example: heroku logs -t --app nodejsdemo</p>
<p dir="ltr">

If the error in the log is H14 - *No web processes running*, it indicates that there is no web processes running and we need to explicitly start one.

heroku ps:scale web=1

Now try the application URL and you could see the issue got resolved. To make sure the web process is running check it using the folloiwing command

heroku ps
Example:
D:\SURESH\nodeworkspace\herokudemo>heroku ps
=== web: `node app.js`</p>
web.1: up for 8m</p>

After testing the application and if you want to bring down the web server , issue the following command.

heroku ps:scale web=0

This will bring the web process to Zero and you will be getting a H14 error in the logs.