Getting started with Node.js, npm, Coffeescript, Express, Jade and Redis

Erwin van der Koogh

To celebrate my move to the Agile Consulting and Training division of Xebia I thought it would be very appropriate to start playing with some hip new technologies.

From their homepages:

Node.js: Evented I/O for V8 JavaScript. (A framework for building completely non-blocking servers in Javascript)
NPM: A package manager for node.
CoffeeScript: A little language that compiles into JavaScript
Express: High performance, high class web development for Node.js
Jade: Node Template Engine
Redis: An open source, advanced key-value store

In this guide I will take very small steps so that you can verify that you are check whether you are still on track.
The result is an extremely performant, scalable and lightweight alternative for web development.

So I installed a brand new Ubuntu 11.4 virtual machine and got started. After following some tutorials, googling and some tweaking this is the step-by-step guide I came up with.

Installing Node.js

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:jerome-etienne/neoip
$ sudo apt-get update
$ sudo apt-get install nodejs

Installing npm

$ apt-get install curl
$ curl | sudo sh

Node.js Hello World

$ mkdir helloworld
$ cd helloworld
$ vi helloworld.js


var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
console.log('Server running at');

$ node helloworld.js


Add Coffeescript

Writing all that javascript is going to lead to lots and lots of brackets, semi-colons and parantheses. Let's use Coffeescript to make our code readable again.

$ sudo npm install -g coffee-script
$ cd ..
$ mkdir hellocoffee
$ cd hellocoffee
$ vi


http = require "http"
http.createServer( (req, res) ->
  res.writeHead 200, {"Content-Type": "text/plain"}
  res.end "Hello Coffee!!"
).listen 1337

console.log 'Server running at'

$ coffee -c
$ node hellocoffee.js

And check again.

Adding Express

Node.js is a very powerful, but it still requires quite a bit of boilerplating to use it for web development. That's where Express comes in. It is build on top of another middleware layer called Connect and gives you much better support in handling requests, rendering, templates and responses.

$ cd ..
$ sudo npm install -g mime qs connect express
$ mkdir helloexpress
$ cd helloexpress
$ express
$ npm install -d
$ node app.js




express = require('express')app = express.createServer()

# Setup configuration
app.use express.static(__dirname + '/public')
app.set 'view engine', 'jade'

# App Routes
app.get '/', (request, response) ->
  response.render 'index', { title: 'Express with Coffee' }

# Listen
app.listen 3000
console.log "Express server listening on port %d", app.address().port

$ coffee -c
$ node app2.js

And check again.
Now that we have Coffeescript working with Express let's add sessions.

Add session support

$ vi

insert after app.use express.static(__dirname + '/public')

app.use express.cookieParser()
app.use express.session {secret: "Coffeebreak" }

insert after app.get '/', (request, response) ->



response.render 'index', { title: request.session.views + ': Express with Coffee and sessions' }

$ coffee -c
$ node app2.js

Check and refresh a few times. You should see the counter increase.

Install Redis

Open a new terminal.

$ wget
$ tar -zxvf redis-2.2.11.tar.gz
$ cd redis-2.2.11/
$ sudo apt-get install build-essential
$ make
$ ./src/redis-server

Add redis session to node

$ npm install -d redis connect-redis

insert after express = require 'express'

RedisStore = require('connect-redis')(express)


app.use express.session {secret: "Coffeebreak", store: new RedisStore, cookie: { maxAge: 60000 } }

$ coffee -c
$ node app2.js

In the Redis terminal you should now see something like:
[25489] 24 Jun 04:57:11 - DB 0: 1 keys (1 volatile) in 4 slots HT.
[25489] 24 Jun 04:57:11 - 1 clients connected (0 slaves), 799032 bytes in use

Check and refresh a bunch of times
kill and restart node

$ node app2.js

Check again and refresh a few more times
Note that you started where you left off.

And that brings us to the end of this guide. As you can see the stack is very powerful and extremely small, lightweight and clean. With all the session information in the Redis store the availability of the entire systems comes down to the availability of Redis. And with these guys working on a proper clustering solution it will be very interesting to see where all this is going in the near future.

Comments (13)

  1. Jan Willem Tulp - Reply

    June 24, 2011 at 4:05 pm

    Great step-by-step guide, looks pretty simple to get started!

    • Erwin van der Koogh - Reply

      June 24, 2011 at 4:19 pm

      Jan Willem,

      Thanks. It is extremely simple to get started. And the load tests are pretty impressive too. Even on my little virtual machine:
      476.85 requests/sec per core with redis only pulling about 2% CPU.

      ab -n 10000 -c 10

      Server Software:
      Server Hostname:
      Server Port: 3000

      Document Path: /
      Document Length: 328 bytes

      Concurrency Level: 10
      Time taken for tests: 20.971 seconds
      Complete requests: 10000
      Failed requests: 0
      Write errors: 0
      Total transferred: 6036460 bytes
      HTML transferred: 3280000 bytes
      Requests per second: 476.85 [#/sec] (mean)
      Time per request: 20.971 [ms] (mean)
      Time per request: 2.097 [ms] (mean, across all concurrent requests)
      Transfer rate: 281.10 [Kbytes/sec] received

      Connection Times (ms)
      min mean[+/-sd] median max
      Connect: 0 0 0.1 0 3
      Processing: 9 21 8.6 18 91
      Waiting: 9 21 8.6 18 91
      Total: 10 21 8.6 18 91

      Percentage of the requests served within a certain time (ms)
      50% 18
      66% 19
      75% 19
      80% 20
      90% 33
      95% 39
      98% 51
      99% 53
      100% 91 (longest request)

  2. links for 2011-06-28 « Bloggitation - Reply

    June 29, 2011 at 8:06 am

    [...] Getting started with Node.js, npm, Coffeescript, Express, Jade and Redis (tags: javascript node.js coffeescript redis programming) [...]

  3. Nigel - Reply

    June 30, 2011 at 3:13 pm

    Great article... I appreciate you for taking the time to compose this walk-through.

    Just one correction, I think the second 'cd redis-2.2.11/' should have been "make"


    • Erwin van der Koogh - Reply

      June 30, 2011 at 6:12 pm

      Thanks Nigel, you are absolutely right ofc 🙂

  4. Nigel Thorne - Reply

    July 5, 2011 at 2:37 pm

    Thanks for your introduction. With a couple of changes I got it running on Heroku. The code is here:

    • Erwin van der Koogh - Reply

      July 6, 2011 at 11:14 pm

      Hey Nigel,

      Thanks for the information. I was already doing the research and first tests to get it running on Heroku. It was (and probably still is) going to be another blog post in the near future with step-by-step instructions.
      Good to know it pretty much works out of the box 🙂

  5. David Herron - Reply

    July 14, 2011 at 7:11 pm

    Very nice. You youngsters don't know how easy it is to install Node these days. Why, in my day we had to download the source and compile it ourselves.

  6. Clifford Heath - Reply

    August 26, 2011 at 5:17 am

    Express refuses to install with edge node.js, unfortunately. Bricks.js was recommended, so I'll give that a whirl.

    Also, your example mentioned Jade, but I don't see it being installed, nor apparently are any Jade templates being used. Small oversight?

    • Erwin van der Koogh - Reply

      August 26, 2011 at 9:33 am

      @Clifford. I am not sure what would be going wrong with the installation. Just took a look at brick.js and while it seems to be more flexible than Express I think Express would be way more suitable if you are building a web stack. Certainly because it is based on Connect, which has lots of plugins for stuff already.

      As for Jade. It comes installed with Express. I did not change the default template that comes installed with Express. Might add a paragraph or so about how to change that. Thanks for the feedback.

  7. Online Win - Reply

    August 30, 2011 at 12:30 pm

    Useful guide - clear and nicely written

  8. TJ - Reply

    November 29, 2011 at 11:58 pm

    you should use NODE_ENV=production, depending on the machine you should get at least ~2500rps with jade

  9. Lou - Reply

    March 11, 2014 at 3:39 am

    When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove me from that service? Thanks!

Add a Comment