A Gentle Introduction to Node.js

Node JS is the latest trend hitting the interwebs, if you haven't heard of it you really should move from under the rock you live under, only kidding, but you really should check it out. This article is meant to give you a really quick introduction/tutorial to node.js and help you get started.

What is it?

Node.js is javascript. Server Side, Event Driven javascript that runs on the obscenely fast V8 engine from Google http://code.google.com/p/v8/.

What is it used for?

Anything server side really, anything that you can imagine; web servers, tcp servers, github use it as a download manager (Nodeload) and others use it for parsing file uploads at super fast speeds (debuggable). Currently I'm playing about with it as a web based backend for a time tracking app I'm writing and this is what this post will be based on.

Getting Started:

To Kick off, clone or download a version:

git clone git://github.com/joyent/node.git

wget http://nodejs.org/dist/node-v0.1.102.tar.gz

then install as follows:
./configure
make
make install
Official instructions here: http://nodejs.org/#download

V8 is included so after that you should be ready to go!

Create a server and handle requests:

Once you have this code, start it up with:

node NodeServer-1.js

and head on to localhost:8000 where you should see a lovely "Hello!" message.

Quick note on modules:

Node is getting quite a following at the moment and as such there are a few libraries/modules springing up alongside it's own modules as well. Node uses the CommonJS module loading system which is a standard way in which modules should be written in order to work with lots of different javascript module systems. Further info can be found here: http://www.commonjs.org/specs/modules/1.0/

Next step; Accepting POST requests:

This took me a while to figure out and I have to say that I'm not overly impressed with the node API documentation, but chances are it's that I need to learn to read a little better! The key to it is that, when the request first comes in, the body hasn't yet been processed so we need to wait for it to come in before we can access it. Wait? Wait? I hear you cry, this is event driven, surely it'll just tell us when it's ready, indeed, you are correct, it does.  So no more polling testing to see if something is ready, we simply give it a callback to process when the data comes in.

Thus:

Here we have two callbacks, one waiting for the data, the other waiting for the data to finish coming in. Then we can use it and write the response. That's the basic of accepting http POST requests, you can test it with:

curl -d "param1=value1" localhost:8000

Displaying Something Useful, AKA templating:

This is all well and good so far, but at some point you're probably going to want to add an interface to it rather than just messing about on the command line. In steps templating, making life a little easier then sending back raw HTML to the client, although this is certainly an option. There are quite a few templating options about that are compatible with Node.js. In this example we'll be using Mustache.js, but I'll list a few here so you can check them out and make a decision on which you'd like to use:

So take your pic! And please let me know of any I've missed out in the comments.

Mustache.js

http://github.com/janl/mustache.js/

The idea behind Mustache is to remove all logic from templates, so there are no ifs, elses, loops. Instead they are replaced with tags which get expanded into values which are provided by an object.

To Use:

git clone http://github.com/janl/mustache.js/

Then copy the mustache.js into the directory you are coding in. This is where I think I may have gone a bit wrong. Requiring in the normal way didn't work for me, as in commonJS modules all vars scoped to the module to stop pollution of the global namespace (a very sensible idea) but as Mustache was created and assigned to a local variable I couldn't simply require it like the previous modules. To fix I opened up mustache.js and removed the var declaration when Mustache is being created.

So:

var Mustache = function() {...

Became

Mustache = function() {...

What this does is make Mustache globally available. Which is a bit naughty, but it worked. Someone please show me the light on this!

All Together Now:

 So now we know how to create a server, accept post data, render responses we can put it all together to make a simple time tracking app, which will route the requests and write the data to a file.  I'll be expanding on this later to make it write to Redis in a later blog post.

Filed under  //

Viewed  // 
times