Skip to main content

Nodejs is an incredible tool that allows us to use javascript beyond the browser.

- What do you mean with that?

According to its official Nodejs website it is:

"It's a JavaScript runtime built with Chrome's V8 JavaScript engine."

Surely you ask yourself something like ... And what does this mean?

Well you see, in the past javascript wasn't even that great, not as open as it is now. Javascript in its conception was originally developed by Brendan eich for netScape and it was strictly linked to browsers.

It was not until 2009 after Google gave the world its Google Chrome browser it freed javascript from its web prison thanks to its V8 JavaScript engine.

- And what is the V8 engine? (sounds like an energy drink)

The engine is the software that interpret JavaScript code and that in turn, executes a script according to the instructions given. All web browsers have a JavaScript engine, Safari uses JavaScriptCore, FireFox uses Spidermonkey, Microsoft Edge uses Chakra and in this case Google Chrome with V8, which is also partly the engine of Nodejs.

Resource of Rainer hahnekamp

The launch of the V8 it marked a turning point in engine history. V8 it replaced the relatively slow JavaScript rendering of the browser.

The reason behind this huge improvement lies mainly in the combination of interpreter and compiler. Today, all four engines use this technique. The interpreter executes the source code almost immediately. The compiler translates the source code into machine code that the user's system executes directly.

You may wonder ...

- At the end of the day, does all this have to do with Nodejs?

Well the relationship is that one day Ryah Dahll, the creator of Nodejs, looking for a way to create web servers that consider the needs of the current web, that is, a huge number of users and all accessing in real time, he found the google v8 engine and made it work outside the browser, that is, in the operating system, and from this moment, it is born Nodejs. and which presents it in the JsConf, a Javascript conference.

Nodejs is special because according to its official website:

"Uses a model of I / O directed to non-blocking events that makes it light and efficient, ideal for real-time applications with intense data exchange through distributed devices"

As you will see, it is not a simple concept, especially if you are starting, so I will start by detailing what Nodejs is:

Nodejs It is a javascript execution environment that is built with the google chrome v8 engine which is a non-blocking and event-oriented operations model.

well this doesn't help much either, but let me explain from this statement.

Nodejs is based on the V8 engine that Google created to interpret javascript in Chrome, Taking advantage of this engine Nodejs, compile the code JavaScript to native language, in addition to having a great execution speed, the simplicity of the interface that Nodejs provides us is so incredible, that starting a server takes less than 20 lines of code, for example:

// The HTTP module is loaded var http = require ("http"); // Create the HTTP server, and define the listening // for requests on port 8000 http.createServer (function (request, response) {// The HTTP header is defined, with the HTTP status (OK: 200) and the content type response.writeHead (200, {'Content-Type': 'text / plain'}); // Respond, in the response body with the message "Hello World" response.end ('Hello World! n ');}). listen (8000); // The URL to access the server is written console.log ('Server in url');

The particularity of Nodejs resides in a piece called EventLoop.

Veras Nodejs arises as a response to a problem with traditional sequential programming. In languages like Java, C ++ or C#, which are based on execution threads, there is a theoretical maximum that is given by the memory consumed by each execution thread on the machine on which it is deployed.

Nodejs instead it is single thread(or from single thread), this single execution thread is called EventLoop and its function is to execute Javascript code which is by nature.

awdResource of Digital paradigm

Before the arrival of entry / exit operations(Input / Output) delegates their execution to subsystems specifically prepared to be processed in the background.

In this way the execution thread is not blocked (non-blocking), while the EventLoop points to the function that will be executed once that offline processing finishes (callback).

A simple way to understand what the EventLoop is to imagine that the system is like a restaurant, in which we have a single worker: a waiter. Our waiter is in charge of seating people at the table and taking their food order (asynchronous task)If the waiter had to prepare the food, we would be blocking him so that he could attend the next table.

Instead, the waiter sends the order to the kitchen (threadPool), in which one of the cooks prepares it. When the food is ready, he will call the waiter to take it to the table, our waiter will have written down the correspondence between the food and the table number to know the place where he has to deliver the food (callback).

I hope this introduction was enough to cause interest in learning more about Nodejs. For more information and examples of applications on this platform you can visit the official documentation of Nodejs.