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!

Posted in Code, Hacking | Tagged , , , , , | Leave a comment

Liverpool and Manchester

Not a football match, but almost the last part of my trip to the United Kingdom.

After Santa Pod I drove east, to Liverpool. Not being av football fan I’m not really sure there were to much to see there, sorry.

I went to the Liverpool Museum, The Beatles Story and just walked around in the city, but didn’t really like it. Don’t know exactly why, I found some nice restaurants in Liverpool Chinatown and a nice pub or two, but ..

Manchester up next!

In Manchester I meat Claudia and Stian Soiland-Reyes and their cute boy Sebastian. Together we got to some location at University of Manchester to celebrate the Mexican independence day; Claudia is from Mexico.

Sebastian, 7 months old, didn’t want to spend the night there, so he and his mother went home quite early, and Stian and I did the celebrating, which included English beer, Mexican food and a least two smashed piñatas before we found a nice English pub for one more beer before it was time to ride the last bus / tram back ‘home’.

The next day Claudia and Stian invited me over for Mexican dinner. Very nice food, and very nice to speak to Stian who I haven’t seen for years (we used to work together in Trondheim)

Thanks Claudia and Stian!

Sorry about the low quality picture; iPhone in the dark …

Posted in Travel | Tagged , , | Leave a comment

FIA/UEM dragracing finals at Santa Pod

After spending one night in the strange town of Weston-super-Mare I drove up to Kettering. A small town located 80 miles from London and close to Santa Pod Raceway where the finals for both FIA (cars) and UEM (motorcycles) European championships are held each year.

Going to the finals was one of the main reasons for this trip, and it was really worth it.

I will not write a lot about results, you can read all about that on eurodragster.com, as the nitro smell in the air and all kinds of race cars and bikes blasting down the strip just has to be experienced!

Love it!!

Posted in Motoring, Photo, Travel | Tagged , , , , | Leave a comment

All the way to Land’s End

From Weymouth I kept on driving west and south all the way down to Penzance.

In Penzance, or more precisely Newlyn, I staid at Panorama Guesthouse, a really nice place located in the steep hills south of Penzance city. A great place, with a lovely and nice people!

Penzance

From Penzance I took a drive through Mousehole and all the way to Land’s End.

Many really narrow roads out on the Cornwall countryside.

Road oustside Mousehole

Contradiction in terms?:

Drift - Please drive carefully

From Penzance I drove, in a horrible rain and wind, up to Weston super Mare, more about that later. (I now have a working UK -> Euro power plug adapter again …)

Music played while writing: Let England shake, PJ Harvey

Posted in Travel | Tagged , | Leave a comment

Weymouth – home of fish and chips

I stayed in Brighton for one night, as I wrote about yesterday, and then drove A27, A31, A35 and finally A354 down to Weymouth.

Just as I left Brighton the rain started to pour down, and the windscreen wipers had to do their work all the way down to the lovely small town of Weymouth.

I had booked a room at the Weymouth Sands Guest House and got a small room up on the top floor, with a nice view of the beach from my window:

Weymouth beach, from my window

Diane at the guest house was very friendly, and told me some history about Weymouth and the surrounding area. She also told me I should go to the Marlboro restaurant and try som fish and chips. I did, and ate at least twice as much as I normally do for dinner. Fantastic food!

The restaurant is located next to the Town Bridge, close to the inner harbour:

Weymouth

A rather crazy thing in Weymouth is the rail road going in the city streets; this once was a combined tram and train line after what I can understand, no trains for me though:

Harbour Line, Weymouth

It was sunny yesterday evening, but as I woke up today the rain was back. I did enjoy a delicious “full English’ by Diane before I left Weymouth.

Should have gone there in the summer!

Sunset over Weymouth beach

… and I should find / develop a Lightroom lens profile for the Tokina 11-16mm, the barrel effect is quite annoying.

Music played while writing: Den blå trikken hjem, Bare Egil Band

Posted in Photo, Travel | Tagged , , | Leave a comment