First Steps with Node.js: exciting stuff

If you are looking for a NodeJS tutorial, visit this post: NodeJS Tutorial with CouchDB and Haml.

During my Saturday morning reading yesterday I fell over something called Node.js.  According to the website

Node’s goal is to provide an easy way to build scalable network programs.

which is kind of exciting, but not mind blowing, however, Ryan Dahl’s GitHub page describes it as

evented I/O for v8 javascript

Which is slightly more exciting. However, it is when you start to play around with it that things begin to get very exciting indeed.

Simon Willison’s latest blog post was the first I’d heard of Node and he offers a very good introduction and tutorial, bringing in a Djanjo slant. Towards the end of the post he mentions database connectivity and when I think about the possibilities with CouchDB my mind explodes with possibilities!

Over at Naked Javascript, there is another great post, offering a more detailed tutorial, including a client that gets a Twitter search. Again, this article is very enthused about Node and its future direction.

Ryan Dahl’s presentation on Node is hugely compelling, and provides excellent insights into the philosophy behind Node: PDF download.

With all this excitement in the air, I thought I better download me a copy and see what all the fuss is about.

Downloading and installing both Google’s V8 JavaScript Engine (which Node is built upon) and Node was effortless. I won’t go into the details of installation here as it is covered in both the articles above and the Node homepage, needless to say that and I had them up and running on a clean Debian VirtualBox install in about 5 – 10 minutes.

I copied and pasted the “Hello World” example from Simon Willison’s article, ran it and I was away.

The code used is extremely simple, as you can see

var sys = require('sys'),   http = require('http');
 
http.createServer(function (req, res) {
 res.sendHeader(200, {'Content-Type': 'text/plain'});
 res.sendBody('Hello World');
 res.finish();
}).listen(8000);
 
sys.puts('Server running at http://127.0.0.1:8000/');

To run I simply ran

$ node hello.js
Server running at http://127.0.0.1:8000/

and then navigated to the page in my browser where I was greeted with a rather plain but not boring “Hello World”

The res.finish() call tells the client that the server has finished and it should consider the message complete.

This started me thinking, it would be possible to complete all the stuff that needs to be sent to the client, finish the conversation with the client, but then still carry on, without losing any efficiency. So I hacked apart the “hello world” app to see what happened if you did stuff after the send. The new code looks like this:

var sys = require('sys'),   http = require('http');
 
var test_func = function() {
 setTimeout(
 function() {
 sys.puts('Called func');
 }, 5000);
}
 
http.createServer(function (req, res) {
 res.sendHeader(200, {'Content-Type': 'text/plain'});
 res.sendBody('Hello World');
 res.finish();
 test_func();
}).listen(8000);
 
sys.puts('Server running at http://127.0.0.1:8000/');

I added the timeout to represent time needed to execute other processes, such as connecting to a  database etc. It is heavily exaggerated but it should give me an idea what’s going on. Running the program now starts off as before:

$ node hello.js
Server running at http://127.0.0.1:8000/

But then when navigating to the page, 5 seconds later the command line reported:

Called func

So it is clearly completing the conversation with the client first, then executing the function.

I have played around with Apache Benchmarking to see how this handles large requests, and it seems to hold up, but obviously more stringent tests are required before including this technology into a production environment.

However, this is still very exciting. It opens up the possibility of having an HTTP server that waits for requests, getting the key information and saying “bye” before processing the request. Something like a queue server, but very fast and very efficient. And when coupled with CouchDB or similar, well, things start to get very interesting.

So I am going to spend the rest of this weekend hacking around with Node, I want to know everything about it, and you should too.

Node is definitely something to keep an eye on.

Update: Just found another good blog post, this time with an example for Long Polling.

Update: If you are looking for a NodeJS tutorial, visit this post: NodeJS Tutorial with CouchDB and Haml.