What are the Special features of Node.js? Why it is widely used?

Harshita Gupta
3 min readJan 25, 2021

One of the Important reasons to use Node.js is already discussed in my other blog.

Let’s understand other key features of Node.js.

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

Event-Driven

Before understanding how event-driven does in node.js. First, understand what actual meaning of event-driven.

In an event-driven architecture, there are two actors: the Event and the Listener. When an event happens one or more listeners listening or attending it and react on it by doing some actions.

In practice, JavaScript in the browser can interact with HTML elements, these elements are event emitters. That means HTML elements are subjects to emit events. Examples of such events are onclick,onkeypress,onmouseover,onload. Listeners are the functions written in JavaScript which react whenever the event happens which is listened to by the listener.

How this event-driven applied to Node.js?

In Node.js there aren’t any HTML elements, so most of the events originate or emits from Processes, interaction with Files, networks, and so on.

In node.js every emitter has a method named “on” which takes at least two arguments :

Name of the event on which listener listen.

A listener function.

server.on("listening", function () {
console.log("Server listening!");
});

Here server is the event emitter, on method register the listener function with an event named listening. As soon as the server starts, the listening event fires, and “server listening” is printed on the console.

These listener functions are usually callback functions.

Non-Blocking I/O model

Before understanding what is non-blocking I/O model, first understands what is Blocking and Non-Blocking.

Blocking

Operations that are non-CPU intensive block the execution of CPU intensive operation is categorized as blocking operations. Such as reading and writing files or request data from the database, basically I/O operations.

But in node.js, I/O operations are not typically referred to as blocking operations. Synchronous methods in the node.js standard library libuv are the most commonly used blocking operations.

Non-Blocking

Operations don’t wait for the completion of others referred to as Non-Blocking operations.

Blocking operations execute synchronously and non-blocking methods execute asynchronously.

All of the I/O methods in the node.js standard library provide an asynchronous version, which behaves as non-blocking and takes callbacks. some methods also have blocking behavior they have names that end with Sync.

Synchronous file read using the file system module:

const fs = require('fs');
const data = fs.readFileSync('/file.md');
console.log("Hello World!");

Equivalent asynchronous file read example:

const fs = require('fs');
fs.readFile('/file.md', (err, data) => {
if (err) throw err;
});
console.log("Hello World")

In the first example code is looking smaller and simpler than the second one but the second line of the first example blocks the execution of the next line until the entire file is read, even it is not related to that file data. If there is an error is thrown then the execution of code can crash. Hence we need to catch that error.

In the second example, the execution of the last line not block by the file read operation. If there is any error thrown then the whole code will not crash and the error can easily be handled by a callback function.

Now we are ready to understand how node.js uses the asynchronous I/O model.

Execution of javascript code is synchronous and is single-threaded but there is something in node.js which helps to run asynchronously and that something is Event-loop.

An important point to remember — Never mix Blocking and Non-Blocking Code.

--

--