Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

A guest post by Pedro Teixeira, a geek, programmer, freelancer, and entrepreneur. He is the author of some Node.js modules, the Node Tuts screencast show, some books about Node.js and overall fervent proclaimer of the Node.js creed. He is also the Co-founder and Partner of The Node Firm, and the organizer of the Lisbon JavaScript Conference.

Node.js was born

In 2009 a young programmer named Ryan Dahl introduced his latest open source project to a small audience in a conference in Berlin. This project combined Google’s V8 JavaScript virtual machine with an event loop, which made it possible to run JavaScript programs without a browser. These programs could use all of the basic Internet protocols, allowing you to create many sorts of different applications, ranging from an HTTP server to a peer-to-peer network of nodes. At the end of his talk Ryan Dahl received a standing ovation, and since then Node.js has seen unprecedented enthusiasm, growth and industry adoption.

Node.js was not the first server-side JavaScript platform — quite the contrary — but it differed from its predecessors in the way that it treated I/O operations: always asynchronously. In fact, what Ryan Dahl did was to translate the web application programming model into the server-side, and with that he opened a box of innovation.

What’s different about Node.js

Typically, waiting for I/O on a server-side application is a synchronous operation. Let’s say that you want to run a SQL query on a database server that is located somewhere on the local network; you could do something like this:

The first line of this program has to send a command to a server on the network and wait for the response, which may take between a few miliseconds and an infinite amount of time to run. Meanwhile, while the network elements are transmitting our message, and the database server is processing our request, the process is waiting. This may be reasonable if this process is only processing one user, but if you, for instance, are dealing with many users at the same time, you will have to create some way of parallelizing these requests. You can do this by using threads or some other trick that allows you to have more than one active call stack at each moment – or, simply, by using an event-driven model.

It’s Like the Web

The web has taught us that the event-driven model works particularly well with JavaScript. JavaScript is a language where a function is treated like a first-class citizen: you can use a function as an argument, store it in a variable and generally pass it around as any other kind of object.

Let’s see how we would implement our SQL query using an event-driven model in JavaScript:

Here we are first declaring a function arbitrarily named onArticle. This function accepts an article object and prints it out.

Then, on line 4, we start our query, but instead of expecting the article to come in the returned value, we pass our onArticle function into the query arguments. The request will then be responsible for invoking our function when the article is back from the server, probably a few miliseconds later. Meanwhile, our process can proceed doing what it needs to do without blocking.

This type of interaction may come naturally to web programmers: they’ve been using function callbacks to do I/O in a browser. For instance, when performing an AJAX call, you provide a function that gets invoked when the call terminates. This is not much different from when you register event listeners to button click:

Besides being able to pass functions around, JavaScript has a powerful construct: a function can access the variables in the scope it was declared in (or in any of the parent ones). In the previous example this is what allows the onClick function to access and modify the count variable, even if that scope has returned.


Besides file-system manipulation, Node itself implements a set of basic network protocols: TCP, HTTP, TLS, HTTPS, DNS and UDP, which means that, in order to efficiently build a useful application on top of Node.js you will always have to use third-party modules. Fortunately, Node.js comes bundled with NPM. NPM is two things: 1) the best package manager I’ve had the privilege to work with and 2) a vast and exponentially growing catalog of open source modules that you can use to help you build your application.


If you’re curious and want to learn how to use Node.js, be sure to watch this Learning Node.js LiveLessons video training by Marc Wandschneider, an experienced developer and entrepreneur. Watch as Marc takes you step-by-step, from the JavaScript basics into building a modern Node.js server-side application.

See below for more Node.js resources from Safari Books Online.

Read these titles on Safari Books Online

Not a subscriber? Sign up for a free trial.

Learning Node.js LiveLessons (Video Training) provides developers with a practical introduction to Node.js, an exciting new platform for developing web and network applications. In this video training, Marc starts by introducing Node.js, installing, and running it. He then gives a quick refresher course on the JavaScript language and its new features. The video then moves into writing JSON servers, web applications, client-side templates, and continues by covering database access to both SQL and NoSQL database servers. After watching this video, you will begin to understand why Node.js inspires so much passion, and they will be able to write fast, compact, and reliable applications and web server.
Node.js is a powerful and popular new framework for writing scalable network programs using JavaScript. Professional Node.js begins with an overview of Node.js and then quickly dives into the code, core concepts, and APIs. In-depth coverage pares down the essentials to cover debugging, unit testing, and flow control so that you can start building and testing your own modules right away.
This book shows you how to transfer your JavaScript skills to server side programming. With simple examples and supporting code, Node Cookbook talks you through various server side scenarios often saving you time, effort, and trouble by demonstrating best practices and showing you how to avoid security faux pas.
Node Web Development gives you an excellent starting point straight into the heart of developing server side web applications with node. You will learn, through practical examples, how to use the HTTP Server and Client objects, the Connect and Express application frameworks, the algorithms for asynchronous execution, and use both SQL and MongoDB databases.

About the author

Pedro Teixeira is a geek, programmer, freelancer, and entrepreneur. Author of some Node.js modules, the Node Tuts screencast show, some books about Node.js and overall fervent proclaimer of the Node.js creed. He is also the Co-founder and Partner of The Node Firm, and the organizer of the Lisbon JavaScript Conference.

Tags: event-driven, Google's V8 JavaScript, Javascript, NodeJS, NPM, Ryan Dahl,

Comments are closed.