The H Speed Guide to Node.js
by Dj Walker-Morgan
Node.js, or Node for short, has become rather popular with web developers in the last year as a platform for their web applications. No one is talking about replacing the entire world of web servers with Node.js based systems, but Node is flexible enough to be able to take on a wide range of tasks. So what makes Node different to preceding web frameworks and platforms? Two words, event-based JavaScript.
Event-Based
The traditional way of handling a web request is to get the request, parse it, wait to load the resources needed from disk to process it, process it (however long that takes), and return a response. Because there is a lot of waiting involved, to handle two or more requests at the same time would require spinning off a thread of execution for each request. The more requests you need to handle, the more threads you spin off and the more resources you consume to manage each thread.
Event-based frameworks take a different approach, but they do require that you code differently. They exploit the fact that many server applications spend their time waiting for I/O, and turn that waiting time into working time. There is only one thread of execution, but the programmer splits up his code into chunks which are called when an "event" occurs. For example, opening a file takes some I/O time, so in event driven systems, we say "please start opening a file, when you are done opening the file, call back to this function." The framework starts the open, makes a note of the required function and waits to be told by the OS that the file is open. When it gets that notification, that event triggers the calling of the required function.
Now, the first reaction could well be: "But doesn't this make my code a mess of interlinking functions?" The answer is yes, if your language isn't expressive enough to handle it. For example, if your chosen language handles anonymous functions, the code could look like this:
The real difference is that the run-time is not stopping while operations complete, but is making a note of what to do next when the underlying operating system says they are done. Event-driven programming can be done in a range of languages, with various degrees of readability and ease – in the case of Node.js, the language of choice is JavaScript.
JavaScript
Node.js is built using the V8 JavaScript engine, the same engine that powers the Chrome browser. It's used in Node to provide the execution environment, complete with Just In Time compilation and other optimisations. The JavaScript language has, historically, had a bad reputation because of the way it has been abused performing animations inside browsers or for not being Java despite the name. But over the last decade developers have been discovering that within JavaScript is a powerful language with its roots in Scheme, Self and Lisp, rather than the procedural languages such as Java, C and Pascal, and it is only a historical accident that saw it wrapped up as if it were a variant of Java. The root of this rediscovery was Douglas Crockford's "JavaScript: The Good Parts", based on Crockford's talk of the same name (see the Google Tech Talk version below) and Crockford's work.
Crockford points out that JavaScript was made to look familiar, but that means that people tend to not learn it, and in the process they miss learning the underlying philosophy. A lot of people feel they know JavaScript because they've had to, at least, use it to add programmatic features to a web page. But many JavaScript coders are still surprised to find, for example, that every object in JavaScript is actually an associative array. JavaScript has had a reputation for being slow, but the recent browser wars saw a huge amount of effort put into accelerating JavaScript in all browsers. This process resulted in JavaScript engines like V8 and a lot of programmers familiar with JavaScript. Combining the new understanding of JavaScript the language and these accelerated JavaScript engines was a potent combination which someone was bound to harness...
Next: The Node.js creation story