Skip to content

Introduction to Node.js concepts. Learn its structure

Today I resolved to write a subject that I am studying in the last days, Node.js concepts. Learn its structure. See the basic about 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, of 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 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 it is that the servers traditional are more robust and complex, utilizing more of machine. With Node.js, we can say that 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 mult-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 is opened a thread to care in this request, any request gains a thread. Of course that 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 is 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 this, we can to 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 of 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 of execute 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 that note it 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 more fast than current web servers, as IIS or Apache.

Below we have a diagram represented how Node works:

core node

To install Node is much simple and I’m going to teach the process as a windows user. First we have to access the site http://nodejs.org and download the program. Today Node is in version 4 and has about 10Mb. The installation is simples and it is only following the installer.

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

node cmd

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. This article the goal was to teach the concept of Node and how it works.

See you later!

Published innode.js

Be First to Comment

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *