I decided to write this article because I could not do any differently. There was no choice, Node.js is definitely a trend now and it would be silly and impossible to not start talking about it. A lot of incredible companies are using this tool to build amazing products and millions of developers are learning to love it.  And there is probably no better way for you to learn about it than with an immediate, simple and full immersion to one of the most amazing technologies you will find out there.

Let’s start learning.

What is Node.js:

When they ask you what Node.js is, you can basically address that question by giving out these three facts about it:

  • Node.js is a Javascript runtime environment built on the Chrome’s V8 JavaScript engine.
  • Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
  • Node package ecosystem: npm, is the largest ecosystem of open source libraries in the entire world.

Sound exciting now right? Well, let’s try to analyze in deep these facts.

Two best friends: Node and V8

V8 is the Javascript engine inside of Node.js that will parse and run your Javascript code. This is the same engine used inside of Chrome to run javascript in the browser.

No V8 no party

Keep in mind that Node.js was not designed to work with any other engine. Its binary code without V8 would be completely useless. A proof of this is the fact that the core implementation and modules that come embedded into Node all rely on the specific V8 interface between C++ and Javascript.

Event-driven + non-blocking I/O

Let’s analyze what it means for a system to use a non-blocking system for I/O by first defining what I/O operations refer to and then by comparing it to the old school blocking type system:
I/O definition: “I/O” refers primarily to interaction with the system’s disk and network

Blocking I/O:

Blocking refers to operations that block further execution until that operation finishes. One example of an app like that would be:

// if getNumber is an API call
console.log("Retrieving number1");
var number1 = getNumber('1');                                     
console.log("Number1 is:", number1); // blocked until first call returns data

console.log("Retrieving number2");
var number2 = getNumber('2');
console.log("Data is:", number2); // blocked until second call returns data

var sum = 1 + 2; // evaluation won't start until number2 is logged
console.log("sum is:", sum);

 

Another example of blocking I/O operations are the ways in which certain web servers handle their IO or network requests, where this is especially true for Java and PHP ones.

If your code is reading from a file or the database, it will block everything after it from executing. And so for a certain amount of time, your machine will be holding into its memory and processing a thread that isn’t doing anything.

The way in which your software will take on other requests while it has stalled will vary. What most server software will do is spawning more threads in order to satisfy additional requests. And this, of course, will require more memory and more processing power.

Non-Blocking I/O:

Non-blocking refers to code that doesn’t block execution.

// console.log is in a callback function
// will only be executed once the call is completed
// in the meantime the execution goes on

console.log("Getting number1");                               
getNumber('1', function(number1) {
  console.log("Number is:", number1); 
});

// same as above
console.log("Getting number2");
getNumber('2', function(number2) {
  console.log("Numebr is:", number2);
});

// sum is printed and so will be the
// two console.log once their API call is done
var sum = 1 + 2; 
console.log("sum is:", sum);

 

Advantages of Non-Blocking I/O:

One advantage of non-blocking I/0 is that you will be able to maximize the usage of a single processor as well as the usage of memory.

Asynchronous, non-blocking servers, like the ones realized using Node.js,  will only make use of one thread to service all requests. This means that an instance of Node will make the most out of a single thread. And here we can also understand how the people behind Node perceived it when developing this tool. The prerequisite was that I/O and network operations were the bottlenecks to overcome. 

When requests will arrive at a Node.js server, they will be serviced one at a time. However, when the code will need to query the DB,  as an example, it will send the callback to a second queue but the main thread will continue running! Now when the DB operation completes and returns, the corresponding callback will be pulled out of the second queue and queued in a third queue where they are pending execution. When the engine will get a chance to execute something else (like when the execution stack is emptied), it will pick up a callback from the third queue and execute it.

 

NPM

Npm represents the node package manager and, to give a quick definition of it, is the world largest software registry. People from all over the world are using it to create, share and use milions of packages. This component of Node.js is primarily made of:

  • The website, where you can discover the most popular packages.
  • The CLI, this is how the majority of developers will interact with node.js.
  • The registry, a large and public database of Javascript software surrounding npm.

Useful cases where you might want to use npm

  • For downloading useful packages for your projects.
  • Share code with other npm users.
  • Finding other developers who are tackling the same problem as you.
  • Create Oraganizations(Orgs) to coordinate your work, coding and other developers who will manage the package.

 

Why Node.js:

In the end, this article has been a brief showcase of what Node.js is and what are its characteristics. As I always say though, I don’t want to only create an interest in a new technology, but I want to create a traction in learning that technology. So here I propose to you a little series of data to finally convince you why you should learn it in 2019:

node.js infographic
node.js infographic

 

 

 

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here