An Introduction to Node.js

THE DEFINITIVE GUIDE

Kaushik Kumar Gattani
THE BINARY REALM
Published in
3 min readSep 13, 2020

--

In this article, we will try to answer some questions regarding Node.js.

  • What is Node.js?
  • Why Node.js?
  • When to use Node.js?
  • When not to use Node.js?

What is Node.js?

“Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.”

It is essentially a C++ program that embeds Chrome’s V8 engine, the fastest JS engine in the world.

What is the runtime?

Runtime is a system used primarily in software development to describe the period of time during which a program is running.

Every browser has Javascript engine that takes a javascript code and converts into code that the computer can understand.

Why Node.js?

So why do we need Node.js and why we do not merely use Vanilla Javascript (or plain Javascript)? Because Vanilla Javascript was never made to work outside the browser, i.e. it was only made for client-side and not for the server-side.

So in 2009, Ryan Dahl created Node. He took chrome V8 engine to run code outside the browser.

(There are other javascript runtime engines like Chakra (Edge) and Spider Monkey (Firefox), but Chrome’s V8 is the fastest.)

What makes Node.js so popular?

  • Single-threaded event-driven loop
  • Non-blocking I/O model

Let’s study these in a bit more detail.

Single-threaded event-driven:

What do we mean by single-threaded? What do we mean by a thread?

In simple language, a thread is a lightweight task. In computer language, it is the smallest sequence of programmed instructions.

So now let’s see how this single-threaded event-driven loop works:

  1. The client sends a request to the server.
  2. Node.js server receives this request and puts them into a queue known as “Event Queue”. It receives requests over an indefinite loop and processes them.
  3. The event loop uses only a single thread.
  4. The event loop checks for any client request present in the event queue leading to two possibilities:

a) No request is present: If no request is present, then it keeps waiting for the incoming request indefinitely.

b) If the request is present, then:

i) It processes the request from the event queue.

ii) If the request does not require any blocking I/O operations, then it processes the complete request and sends back the response.

iii) If the request requires some blocking I/O operations like interacting with the file system or database, then it follows the following approach: -

  • Checks threads availability from the internal thread pool.
  • One of the available thread is picked, and the client request is assigned to that thread.
  • This thread takes the request, processes it, performs the blocking operations and sends the response to the event loop, which then sends this to the client.

Single-threaded loop advantages:

  1. More and more concurrent request can be easily handled.
  2. Less thread means less use of memory and other resources.

Non-blocking I/O model:

Unlike javascript, Node.js is asynchronous, which ensures the non-blocking code execution. Asynchronous code executes without having any dependency and no order. So, non-blocking code does not prevent the execution of a piece of code.

Other advantages of Node.js:

  1. Highly scalable and fast.
  2. Node Package Manager or NPM: It is a package manager for Node. A package contains all the files that we need for a module ( javascript libraries that can be included in a project). The https://www.npmjs.com/ hosts thousands of open-source package available for everyone for free.

When to use Node.js?

  • It is perfect for building superfast and scalable data-intensive apps.
  • Can be used for data streaming.
  • Real-time chat application.
  • Server-side web application.

When not to use Node.js?

  • Node.js is not suitable for applications with heavy server-side processing, i.e. CPU intensive applications. E.g., audio and video generations are some of the concurrent requests that can’t be handled properly by Node.js.
  • For such cases, we need to use Python, Ruby on rails or PHP.

Some important information regarding Node:

  • Node is not a programming language.
  • Node is not a framework. It is a runtime for executing Javascript code.

Some companies that are using or at some point used Node:

  • Netflix
  • Uber
  • PayPal
  • eBay

--

--