My name is Michal Sporek. I am a contractor in IT (software development). I am available for IT contracting jobs working remotely (telecommuting).

Please visit my site to learn more...

Learning node.js with Professional Node.js - book review

Node.js is a technology that is growing in popularity on the market within the last years. http://nodejs.org/ is basically an asynchronous JavaScirpt framework which is mostly meant for creating scalable internet applications, HTTP servers, TCP servers, applications based on sockets duplex communication, and many others. The power that Node.js gives to every developer is the ability to rapidly develop internet servers with a few lines of code and you can be sure that those servers can handle multiple requests at the same time because of the asynchronous nature of Node.js. Actually the asynchronous way of working is the point most-commonly brought up by Node.js fanatics. Simplicity is a power of Node.js as well - the core things within the framework are kept to minimum and the existing APIs are simple too. Installation, configuration and deployment is simple with Node.js and that is another strength of this technology.

To demonstrate how easy it is to create a simple HTTP server with Node.js please have a look at the below sample code:

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('This is a very first HTTP server build with Node.js...\n');
}).listen(1400, '127.0.0.1');


The short code snipper shown above is enough for creating a HTTP server running on the port 1400 of the localhost, and you can check yourself that the server responds to the requests if you run it, and then you open the address: http://127.0.0.1:1400 in your webbrowser. The server will write text "This is a very first HTTP server build with Node.js..." as a response to your request.

Another example to show how easily it is to develop a simple TCP server with node:

var server = require('net').createServer(function(socket) {
    socket.setEncoding('utf8');
    socket.write("I am ECHO server, what you will write, I will respond with the same! Type 'quit' to finish.\n");
    socket.on('data', function(data) {
        if (data.trim().toLowerCase() === 'quit') {
            socket.write('This is the end.');
            return socket.end();
        }
        socket.write(data);
    });
}).listen(4000);


The short snippet shown above is a fully working TCP server. Creating such a server in Node.js could be done in a few minutes, the code is very simple - what is does is to simply listen for TCP connections on port number 4000, and then when a connection comes the server displays info about itself saying to the client that it is an ECHO server. And then each line the TCP client sends to the server, the server will respond with the same. When "quit" is send by the client, then the TCP server will terminate.

I have been working with Node.js since some time developing various internet applications with it, learning to use it, and the more I am profficient with it, the more I adore it. Recently I had an opportunity to read a very interesting book about Node.js programming which I believe would be a good lecture for anyone interested in the subject. The book "Professional Node.js. Building Javascript Based Scalable Software" by Pedro Teixeira is a comprehensive guide to building Node.js applications.


The book is structured in a very nice way, the author starts with a chapter about how to install Node.js on a machinewith Windows, and they he introduced the basic concepts of Node.js which is good if you are a very beginner in this technology. The next chapters go deep into more specific capabilities of the framework, with events, Event Emitter API, and scheduling the execution of functions with timers. With the next chapters you can learn how to operate with files, how to create and control external processes, reading and writing data to streams, creating TCP/IP server and client applications, developing scalable HTTP servers, and using Node.js to work with other protocols like UDP.

In addition to the basic subjects, author discusses more advanced things like using Node.js togeher with Socket.io for building scalable network applications with WebSockets that allow for duplex communication between the server and the client. This is a very interesting aspect for me as it allows to rapid building of excellent efficient real-time solutions.

Many experienced developers of network applications are skeptical about node.js, they say it is a "cancer" which is destroying brains of webdevelopers, they accuse it of having the kind of architecture that is breaking the simplicity and the classical separation of roles. It is true that in the standard solutions there is a split of roles between the HTTP server that is just doing its job, and the application that is just being interpreted by the server. With Node.js that split is actually broken, because the web server and the web application become one Node.js unit. And I believe that this is the power of Node.js! It allows you to rapidly build and develop network applications without the need to care about HTTP servers, without a need for an advanced installation and configuration of HTTP servers. Deployment and configuration of web servers can be an advanced task that not every developer can handle. Web servers contain bugs, they have many built-in constraints, they are heavy-weight, and very offen they are not efficient. Classical web servers have many limitations when it comes to asynchronous processing of requests. On the contrary Node.js is simple, easy to deploy and easy to configure. It gives you a total control over how you handle web requests, it doesn't have the constraints that web-servers come with, and it allows you to build scalable, asynchronous applications in a very easy way - much easier than with 99% other technologies on the market.

Posted by Michal Sporek