Deploying Node.js Application in the Heroku Cloud


n this post, you will learn how to write a simple Node.js applicaiton, share your project source codes on GitHub and deploy your applicaiton to the Heroku cloud hosting.

1. Node.js

In this section, we will try to create a simple node application.

1.1 What is Node.js?

Node.js or simply Node is a platform built on V8 JavaScript Engine. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. It is good for building scalable Internet applications.

1.2 Setup Node Environment

Download the installation file here.

After installation, you can able to run the following commands.

Check the node version:

[[email protected]] ~ $ node -v

Check the npm version:

[[email protected]] ~ $ npm -v

npm stands for node package manager. It is a package manager tool for node. You can use npm to install node libraries. It acts like rpm in Linux, apt-get in Ubuntu.

1.3 Write Your First Node Application

This is a sample node application which acts as a HTTP server and echo “Hello World” string when the page is accessed.

Create “hello-world” app folder

[[email protected]] apps $ pwd
[stanleyn[email protected]] apps $ mkdir hello-world
[[email protected]] apps $ cd hello-world	

[[email protected]] apps $ pwd

Create “server.js” file

var http = require('http'),
    os = require('os'),
    port = process.env.PORT || 8000;

http.createServer(function (request, response) {
    response.writeHead(200, {
        'Content-Type': 'text/plain'
    response.end('Hello Worldn');

console.log('Server is running at http://' + os.hostname() + ":" + port);

Run Your App Using “node” Command

[[email protected]] hello-world $ node server.js 
Server is running at http://local:8000

Test Your App in Browser

Test Your App in Command-line

[[email protected]] ~ $ curl -i http://localhost:8000/
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Transfer-Encoding: chunked

Hello World

2. Git and GitHub

In this section, we will try to use git for the file version control and use github for the codes sharing to the public.

2.1 What is Git?

Git is a distributed version control system. It keeps every versions of your project source codes in an efficient way. To learn more, please take a look here.

2.2 Install Git

Download the installation file here.

Check the git version:

[[email protected]] ~ $ git --version
git version

2.3 What is GitHub?

GitHub is a web-based hosting service for software development projects. It is the best place to share code with friends, co-workers and developers in public communities.

GitHub is free for all public open source projects.

2.4 Commit Your Codes in GitHub

Create Public Repository in “node-hello-world”

Initialize Your Project

[[email protected]] hello-world $ git init
Initialized empty Git repository in /Users/stanleyn/Projects/node/apps/hello-world/.git/

Commit Your Code from “localhost” to “github”

[[email protected]] hello-world $ git remote add origin [email protected]:stanleyhlng/node-hello-world.git
[[email protected]] hello-world $ git remote show origin
* remote origin
  Fetch URL: [email protected]:stanleyhlng/node-hello-world.git
  Push  URL: [email protected]:stanleyhlng/node-hello-world.git
  HEAD branch: master
  Remote branch:
  master new (next fetch will store in remotes/origin)

[[email protected]] hello-world $ git status
# On branch master
# Initial commit
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#	server.js
nothing added to commit but untracked files present (use "git add" to track)

[[email protected]] hello-world $ git add server.js 
[[email protected]] hello-world $ git status
# On branch master
# Initial commit
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#	new file:   server.js

[[email protected]] hello-world $ git commit server.js -m "initial check-in"
[master (root-commit) 3038fed] initial check-in
 1 files changed, 12 insertions(+), 0 deletions(-)
 create mode 100644 server.js	

[[email protected]] hello-world $ git push origin master
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 680 bytes, done.
Total 5 (delta 1), reused 0 (delta 0)
To [email protected]:stanleyhlng/node-hello-world.git
 8a5c579..7bfc824  master -> master

Browse Your Committed Project in GitHub

You can check my committed project here

3. Heroku Cloud Services

In this section, we will try to use heroku to host our project in public.

3.1 What is Heroku?

Heroku is a cloud application platform. It lets developers spend most of their time on their application code instead of managing servers, deployment and operations.

3.2 Install Heroku Tools

Login Heroku:

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

3.3 Create Your Application on Heroku

Create Your Application on Heroku:

[[email protected]] hello-world $ heroku apps:create node-hello-world-stanleyn
Creating node-hello-world-stanleyn... done, stack is cedar | [email protected]:node-hello-world-stanleyn.git
Git remote heroku added

3.4 Create package.json File

[[email protected]] hello-world $ cat package.json 
   "name": "node-hello-world-stanleyn",
   "version": "0.0.1",
   "engine": {
        "node": "0.6.x",
        "npm": "1.1.x" 
   "dependencies": {

[[email protected]] hello-world $ git add package.json
[[email protected]] hello-world $ git commit -m "added package.json"

3.5 Declare Process Types With Foreman/Procfile

Create Procfile:

[[email protected]] hello-world $ cat Procfile 
web: node server.js	

[[email protected]] hello-world $ git add Procfile
[[email protected]] hello-world $ git commit -m "added Profile"

Start locally using Foreman:

[[email protected]] hello-world $ foreman start
16:57:42 web.1     | started with pid 6906
16:57:42 web.1     | Server is running at http://localhost:5000

Test locally using curl:

[[email protected]] ~ $ curl -i http://localhost:5000/
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Transfer-Encoding: chunked

Hello World	

3.6 Deploy Your Application from Local to Heroku

[[email protected]] hello-world $ git push heroku master
Counting objects: 33, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (28/28), done.
Writing objects: 100% (33/33), 3.39 KiB, done.
Total 33 (delta 9), reused 0 (delta 0)

-----> Heroku receiving push
-----> Node.js app detected
-----> Resolving engine versions

       WARNING: No version of Node.js specified in package.json, see:

       Using Node.js version: 0.4.7
       Using npm version: 1.0.106
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
       Dependencies installed
-----> Discovering process types
       Procfile declares types -> web
-----> Compiled slug size is 2.9MB
-----> Launching... done, v3 deployed to Heroku

To [email protected]:node-hello-world-stanleyn.git
 * [new branch]      master -> master

Scale the web process:

[[email protected]] hello-world $ heroku ps:scale web=1
Scaling web processes... done, now running 1

Check the app process status:

[[email protected]] hello-world $ heroku ps
=== web: `node server.js`
web.1: up for 6s

Tail the app process log:

[[email protected]] hello-world $ heroku logs --tail
2012-07-19T09:09:35+00:00 heroku[api]: Release v4 created by <YOUR-EMAIL-ADDRESS>
2012-07-19T09:09:35+00:00 heroku[api]: Deploy 226a7cf by <YOUR-EMAIL-ADDRESS>
2012-07-19T09:09:35+00:00 heroku[web.1]: State changed from crashed to starting
2012-07-19T09:09:35+00:00 heroku[slugc]: Slug compilation finished
2012-07-19T09:09:36+00:00 heroku[web.1]: Starting process with command `node server.js`
2012-07-19T09:09:37+00:00 app[web.1]: Server is running at http://b98cd371-32d4-4ef5-b6d0-a6af141d6944:29234
2012-07-19T09:09:37+00:00 heroku[web.1]: State changed from starting to up
2012-07-19T09:01:10+00:00 heroku[slugc]: Slug compilation started

3.7 Test Your Application on Heroku


4. Conclusion

In this post, you have learnt how to setup a node environment as well as create a simple node application. Then, you know how to use git to keep versions of your files and share your project codes on GitHub. Finally, you find the way to deploy your project on public cloud hosting and share it in public.

5. References


  1. Sky Reply
    Nice summary!

Leave a Reply


captcha *