Introduction

Late in 2009, Ryan Dahl announced a technology named Node.JS (http://nodejs.org/) at a JavaScript conference in Berlin. Interestingly, and to the surprise of the attendees, this technology wasn’t designed to run in the browser, the land that JavaScript had conquered and that many developers thought it would always be confined to.

This technology was about running JavaScript in the server. That simple phrase immediately sparked the imagination of the audience, which celebrated the announcement in standing ovation.

If done right, we could write web applications in just one language.

That was, undoubtedly, the first thought in everyone’s minds. After all, to produce a rich and modern web application, one must be proficient with JavaScript, but server technologies are varied and require specialization. As an example, Facebook recently revealed that its codebase has four times the number of lines of JS than PHP, its back-end language of choice.

But what Ryan was interested in showing went beyond that simple yet powerful premise. Ryan showed that the “hello world” program of Node.JS creates a web server:

var http = require(‘http’);

var server = http.createServer(function (req, res) {

  res.writeHead(200);

  res.end(’Hello world’);

});

server.listen(80);

It so happens this webserver is not just a toy, but a high-performance web server that happens to fare just as well (or even better) than established and tested software like Apache and Nginx in a multitude of scenarios. ...

Get Smashing Node.js: JavaScript Everywhere, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.