On node.js: Part 0 - Introduction

[Posted by Urban Hafner on 26 Sep 2009]

node.js is JavaScript for the server side. There are other server side JavaScript frameworks (like narwhal or helma), too. But I don’t like them and here’s why: All of these other solutions try to introduce threads as a way of allowing many different connections (if we assume we are talking about a web server). This is bad in several ways:

It’s threads!

Using threads is very, very hard in any language. It’s almost impossible to write a program that uses threads (in a language that has shared mutable state). Also, threads are rather expensive. Both in startup and in memory footprint.

It’s not the JavaScript way

Using threads isn’t really the JavaScript way. JavaScript in the browser doesn’t have threads. Introducing them on the server side doesn’t really “break” the language, but it doesn’t really work the way JavaScript normally works. So, you’re totally switching the paradigm and you can’t really use the knowledge and intuition you already have.

The Event Loop

So what’s the “proper” way to do things in JavaScript? Events, of course! When you run JavaScript in the browser it isn’t in control of the browser, but it only runs in certain intervals or when certain events happen. This works, because the browser has an event loop running that switches between all the different JavaScript code snippets that are supposed to run at the moment.

How does that translate to the server side?

So, why would you need that on the server side? Well, if you’re trying to work on the server side, you need access to the hard disk or have network connections. Compared to CPU and RAM usage that’s really, really, really, …, really slow (many orders of magnitudes in fact). And if you’re trying to run a web server for example that’s bad, because you don’t have threads and you would block all other connections.

And that’s where node.js steps in. For example, if you’re trying to open a file node.js doesn’t block. You just ask node.js to open the file and tell you when it’s done:

node.fs.readdir("/").addCallback(function (files) {
  puts(files);
});

If you’re coming from an language but JavaScript this seems very weird. But it has the following advantages:

  1. It works just like JavaScript in the browser. You attach events just like you do there.
  2. It gives you a form of concurrency (at least for I/O) without the headaches of using threads.

Now for the big example: The web server

Now that we’ve heard of all the good things that is node.js, let’s see a bigger example. How about a web server that returns “hello world”:

node.http.createServer(function (request, response) {
  response.sendHeader(200, {"Content-Type": "text/plain"});
  response.sendBody("Hello World\n");
  response.finish();
}).listen(8000);

Wait a minute! That’s just too short, that’s not a real example. OK, well, … you’ll have to wait for the next part of the series where I’ll walk you through some bigger example (probably a video streaming site or something similar).

But I want more, NOW!

But if you’re interested in more code you can find more information in the following places:

  1. The mailing list
  2. The wiki

If there are any questions don’t hesitate to ask on the mailing list.

Tags nodejs, node.js, javascript, event, events, io, v8, server, server-side, server side

blog comments powered by Disqus