Blog

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

24 Jun, 2011

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 http://npmjs.org/install.sh | sudo sh

Node.js Hello World
$ mkdir helloworld
$ cd helloworld
$ vi helloworld.js

insert
[javascript]
var http = require(‘http’);
http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello World\n’);
}).listen(1337);
console.log(‘Server running at http://127.0.0.1:1337/’);
[/javascript]
$ node helloworld.js
check http://127.0.0.1:1337
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 hellocoffee.coffee

insert
[javascript]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 http://127.0.0.1:1337/’
[/javascript]
$ coffee -c hellocoffee.coffee
$ node hellocoffee.js

And check http://127.0.0.1:1337 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

check http://127.0.0.1:3000
vi app2.coffee
insert:
[javascript]
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
[/javascript]
$ coffee -c app2.coffee
$ node app2.js

And check http://127.0.0.1:3000 again.
Now that we have Coffeescript working with Express let’s add sessions.
Add session support
$ vi app2.coffee
insert after app.use express.static(__dirname + ‘/public’)
[javascript]app.use express.cookieParser()
app.use express.session {secret: "Coffeebreak" }
[/javascript]
insert after app.get ‘/’, (request, response) ->
[javascript]request.session.views++[/javascript]
replace
[javascript]response.render ‘index’, { title: request.session.views + ‘: Express with Coffee and sessions’ }[/javascript]
$ coffee -c app2.coffee
$ node app2.js

Check http://127.0.0.1:3000 and refresh a few times. You should see the counter increase.
Install Redis
Open a new terminal.
$ wget http://redis.googlecode.com/files/redis-2.2.11.tar.gz
$ 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
vi app2.coffee

insert after express = require ‘express’
[javascript]RedisStore = require(‘connect-redis’)(express)[/javascript]
change
[javascript]app.use express.session {secret: "Coffeebreak", store: new RedisStore, cookie: { maxAge: 60000 } }[/javascript]
$ coffee -c app2.coffee
$ 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 http://127.0.0.1:3000 and refresh a bunch of times
kill and restart node
$ node app2.js
Check http://127.0.0.1:3000 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.

guest
14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jan Willem Tulp
11 years ago

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

trackback

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

Nigel
10 years ago

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”
Cheers
Nigel

Nigel Thorne
10 years ago

Thanks for your introduction. With a couple of changes I got it running on Heroku. The code is here: http://github.com/NigelThorne/HerokuNodeCoffeeExpressRedisExample

David Herron
10 years ago

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.

Clifford Heath
10 years ago

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?

Online Win
10 years ago

Useful guide – clear and nicely written

TJ
TJ
10 years ago

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

Lou
Lou
8 years ago

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!

piccosoft
2 years ago

Your blog is very nice… I got more information about your blog page… Thanks for sharing your information…

Explore related posts