Introduction To Node.js

JSHello! How are you? Today I resolved to write a subject that I am studying in the last days, Node.js. Some people ask me if I have left the .Net and my answer is not, I love .Net. I resolved to study Node.js to learn more about a language that it is doing success.

Well, my objective is write a series of articles which go from basic until the creation of a web server in three parts and the article of today I’m going to tell what’s and how it work and why is so well respected.

What is Node? Is a new language? Is a new framework? No! Node.js is a interpreter of JavaScript code that it is in the server, very fast and flexible. Can we have JavaScript Code in the server? Yes, in he same way that you develop in front-end utilizing JavaScript, you also can develop in back-end. Of course, in the server you cannot handle with HTML element. With Node.js we can do all that C# does. For example, can you imagine to do this utilizing JavaScript?

Many people think that Node.js is only for web like IIS or Apache. As much in IIS or Apache after installation, you will have a page ready to be accessed, already in Node.js is different. You are who creates the web server or other thing of your interest. Other difference is that the traditional servers  are more robust and complex, utilizing more of machine. With Node.js, we can say that it is extremely simple, however still much safe and fast.

Node is single-thread and I/O non-blocking. This was a first thing that I heard to speak about Node and I thought strange. How a development platform can be single-thread?

Well, foremost we have to know how IIS or Apache works as multi-thread and then we can compare them with Node.js being single-thread.

Every time that a request arrives in web server of multi-thread type, it open a thread to care in this request, any request gains a thread. Of course this thread number is not infinity, otherwise that would be perfect world. There is a pool of thread and when all are busy cannot help new requests. The thread that attends a request gains some resource, for example, some memory to attend the request. If the request has a process of I/O (data base, files or others), the thread stays busy until the end. After that, the request is prepared to be released to client. Small systems work well this way, already for big systems can be a bad way. Because many server resources are utilized and normally there are requests waiting to be attended.

Already in Node.js changes the paradigm, it utilized the JavaScript Event and this is easy for who is acquainted with Java Script, because, it is a resource that utilize the callback functions concept. Because of this, we can say that anything in Node is asynchronous.

Node begins by a limited thread pool to receive the client requests. When a request arrives, it’s placed within a stack called “Event Queue”. The responsible by catch the request, to process and return for client is called “Event Loop” that is the heart of Node and it works as single-thread. However, if there is a process I/O, the Event Loop stops executing and calls a internal thread pool, done in C++, to execute the process and return for Event Loop when finish. But the great difference that we have noted is that the Event Loop does not wait the return of Internal Thread Pool, it goes to catch other request. With this concept Node.js is very fast than current web servers, as IIS or Apache.

Below we have a diagram representing how Node works:

To install Node is much simple and I’m going to teach the process as a windows user. Firstly, we have to access the site and download the program. Today Node is in version 4 and has about 10 MB. The installation is simple and it is only following the installer.

After install, you will see a Node command according with the following image:

Other great change that Node brings mainly for who is used to it with .NET, it is that we do not have the VS where with F5 your site begins run. Here anything is by command. You will get used!

To the first article I stop here. In next article we are going to create a web server. The goal of this article was to teach the concept of Node and how it works.

Similar Articles