image

Sascha Depold

Engineering Manager

Blog

Node.JS + Express on Snow Leopard

Hey folks! Today I wil take some time to get started with Node.JS and the Express framework. You are maybe interested in how to get this stuff run on Snow Leopard. You need to have XCode installed.

So here is what I did:

Prerequisite

Before diving into the NodeJS world I highly recommend you the usage if kiwi. Kiwi is a NodeJS package manager.

git clone git://github.com/visionmedia/kiwi.git
cd kiwi
sudo make install
sudo kiwi install kiwi

You will probably ask why you should install kiwi with kiwi after you already installed it via the make file. Well,… I actually don’t have a clue, but reinstalling it via kiwi will give you the possibility to update it later.

Installation of NodeJS

Due to the package manager, installing NodeJS is an ease:

sudo kiwi install node

UPDATE: Because it’s no longer possible to install node via kiwi, here are some updated instructions for getting node started:

git clone
http://github.com/ry/node.git
cd node
./configure
make
sudo make install

This will take some time… so lay back and drink some coffee… or beer … or whatever!

After kiwithe script gives you back control to your shell, you should be able to run the node command.

[~] node
No script was specified.
Usage: node [options] script.js [arguments]
Options:
-v, —version print node’s version
—debug[=port] enable remote debugging via given TCP port without stopping the execution
—debug-brk[=port] as above, but break in script.js and wait for remote debugger to connect
—v8-options print v8 command line options
—vars print various compiled-in variables Enviromental variables: NODE_PATH ‘:’-separated list of directories
prefixed to the module search path,
require.paths.
NODE_DEBUG Print additional debugging output.
Documentation can be found at http://nodejs.org/api.html or with ‘man node’

You should also be able to run the Hello World example from NodeJS. I tested it and I get some errors. Here is a corrected version:

var sys = require(‘sys’), http = require(‘http’);
http.createServer(function (req, res) {
setTimeout(function () {
res.sendHeader(200, {‘Content-Type’: ‘text/plain’});
res.write(‘Hello World’);
res.close();
}, 2000);
}).listen(8000);
sys.puts(‘Server running at http://127.0.0.1:8000/’);

Installation of Express

The NodeJS framework express is a Sinatra touched web framework on top of NodeJS. It allows you to just specify some routes (so URIs) and a response you want to give to the browser.

kiwi -v install express

Finished that you can try to run the following lines of code:

var sys = require(“sys”),
kiwi = require(“kiwi”),
express = kiwi.require(‘express’)

get(‘/’, function(){
this.redirect(‘/hello/world’)
})

get(‘/hello/world’, function(){
return ‘Hello World’
})

run()

Just save it in a file and run it with the node command.

So… that’s it! Let me know if something went wrong during the installation process.

Ah and… here are some links to the things: