Getting started with Heroku and Node.js

Heroku is a cloud platform supporting Node.js, Ruby, Python, Scala, Clojure and PHP.

The Cloud seems to be the (buzz)word of the year, and Node.js is cool so I wanted to develop a small application as a test.

Note: I develop on OS X and this lightweight tutorial is OS X Lion specific, you probably should read Getting Started with Node.js on Heroku/Cedar, but it dosen’t work out of the box on OS X!

Ok, here we go:

1. Sign up for Heroku
Just go here and do what Heroku ask you to.

2. Install the Heroku toolbelt
You find the toolbelt for OSX here. Download the pkg file and install it.

3. Install Node.js compatible with Heroku
I normally use Homebrew to install software like Node.js, but Heroku don’t support Node.js version 0.6.15 which is what I got from brew.
Here is a list of supported versions on Heroku

The solution is NVM, which may be installed with like this:

git clone git://github.com/creationix/nvm.git ~/.nvm

NVM is implemented as a bash function. Source it in .bash_profile or similar to have nvm available all the time:

. ~/.nvm/nvm.sh

I installed version version 0.4.7 which is the default version on the Heroku Cedar plattform:

nvm install v0.4.7

It is probably no surprise the Node Version Manager can manage different Node.js versions installed on the same machine, but for now one install is great! (running ‘node -v’ should work by now, giving you v0.4.7)

4. Write your web application

As a test, and because I need the data from time to time, I wrote a small application which maps Norwegian postal codes to places:

var express = require('express');
var app = express.createServer(express.logger());
 
app.get('/', function(request, response) {
        response.send('Hello.  Please give me a Norwegian postal ' +
                      'code (postnummer) and I will give you the place');
});
 
app.get('/:pc', function(request, response) {
        if (p.hasOwnProperty(request.params.pc)) {
                response.send(p[request.params.pc]);
        }
        else {
                response.send('Invalid postal code (postnummer)',404);
        }
});
 
var port = process.env.PORT || 3000;
app.listen(port, function() {
        console.log("Listening on " + port);
});
 
// add data here
 
var p = new Array();
p['0001'] = "OSLO";
p['1300'] = "SANDVIKA";

The p array has close to 4600 elements, and I generate the script with some curl and perl magic. Feel free take a look at my code on GitHub.

5. Create a package.json file

{
  "name": "node-posten",
  "version": "0.0.1",
  "engines": {
	"node": "0.4.7"
  },
  "dependencies": {
  	"express": "2.2.0"
  }
}

I have explicit set node and express versions, the defaults should work ok for now though.

6. Install dependencies on your Mac

npm install

This installs the needed libraries into node_modules, a directory I added to .gitignore because it is local to my installation.

7. Declare process type for Foreman

I added this to my Procfile:

web: node web.js

Test the application running on you Mac

foreman start

You should now have your application available on localhost:5000.

http://localhost:5000/1300 gives me ‘SANDVIKA’ *whee!*

8. Commit everything to git

Something like:

git init
git add .
git commit -m "Initial commit"

9. login to Heroku

heroku login

And follow the on screen instructions

9. Deploy to Heroku

Create application on Heroku Cedar:

heroku create --stack cedar

Then push the application to Heroku with pure git:

git push heroku master

I find this way of deploying pure beauty!

10. Scale webprocesses on Heroku

One process is free, and should do the trick for now:

heroku ps:scale web=1

11. Sit back and enjoy your new cloud app

Isn’t it great?

Thank you people over at Heroku!

More or less related posts:

This entry was posted in Code, Hacking and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *