Blog Posted October 22, 2012

Deploying an Express App to Heroku

Some coworkers expressed interest in deploying Express apps to Heroku. These instructions seek to provide a basic overview, though Heroku offers much more robust documentation in its dev center.

Step 1: Create a Heroku account.

Step 2: Install the Heroku Toolbelt, which includes the Heroku command line client, Git, and Foreman.

Step 3: Log in by entering the following in the command line:

1
heroku login

Step 4: Install Node.js.

Step 5: Create an Express app.

Let’s call it heroku-demo:

1
mkdir heroku-demo

Define the application dependencies via a heroku-demo/package.json file:

1
2
3
4
5
6
7
8
9
{
 "name": "heroku-demo",
 "description": "Basic Express.js/Heroku demo",
 "version": "0.0.1",
 "private": true,
 "dependencies": {
   "express": "3.x"
  }
}

Install the package:

1
npm install

Set up the app via a heroku-demo/app.js file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var express = require("express");
var app = express();

// Set up a URL route
app.get("/", function(req, res) {
 res.send("Heroku Demo!");
});

// bind the app to listen for connections on a specified port
var port = process.env.PORT || 3000;
app.listen(port);

// Render some console log output
console.log("Listening on port " + port);

Now, you can run your app locally and view it in your browser at http://localhost:3000:

1
node app.js

Step 6: Declare your app’s process types with a heroku-demo/Procfile so that it can run with Foreman:

1
web: node app.js

This declares a “web” process, as well as the command needed to run it. You can test that your Procfile/Foreman works:

1
foreman start

Step 7: Make heroku-demo a Git repository:

1
2
3
git init
git add .
git commit -m "Initial commit"

Step 8: Deploy heroku-demo to Heroku.

Create a Heroku app:

1
heroku create

Deploy the code to Heroku:

1
git push heroku master

Step 9: View your Heroku-hosted app in your web browser:

1
heroku open

Some Additional Heroku CLI Tips

Make your app available at a custom newname.herokuapp.com subdomain:

1
heroku apps:rename newname

You can also use custom domains. See the Heroku documentation.

View environment variables:

1
heroku config

More Heroku environment variable documentation.

Add environment variables:

1
heroku config:add NODE_ENV=production

View logs:

1
heroku logs

View running processes:

1
heroku ps

Learn more about the Heroku command line client:

1
heroku help