Getting to know about Node.JS

Sandini Pitawala
4 min readMar 21, 2021

--

Node.js was written initially by Ryan Dahl in 2009.

It’s created with the aim of creating real-time websites with push capabilities (websockets).

What is Node.js?

  • Node.js is an open source server environment
  • Node.js is free
  • Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • Node.js uses JavaScript on the server
Node.js = Runtime Environment + JavaScript Library

Why Node.js?

Opening a file on the server and returning the content to the client is a typical activity for a web server. A file request is handled in PHP or ASP as follows:

  • Sends the task to the computer’s file system.
  • Waits for the file system to open and read it.
  • Returns the content to the client.
  • Ready to take on the next task.

A file request is handled in Node.js as follows:

  • Sends the task to the computer’s file system.
  • Ready to take on the next task.
  • The server returns the content to the client after the file system has opened and read the file.
  • Node.js skips the waiting and instantly goes on to the next request.

Node.js is a memory-efficient single-threaded, non-blocking, asynchronous programming language.

Features of Node.js

The following are some of the main features that make Node.js the software architect’s first choice.

  • Asynchronous and Event Driven
  • Very Fast
  • Single Threaded but Highly Scalable
  • No Buffering
  • License

Concepts

Where to use Node.js?

The places where Node.js is proving to be an excellent technology partner are as follows.

  • I/O bound Applications
  • Data Streaming Applications
  • Data Intensive Real-time Applications (DIRT)
  • JSON APIs based Applications
  • Single Page Applications

Where not to use Node.js?

Node.js is not recommended for CPU-intensive applications.

What is a Node.js Loop?

  • Node.js files contain tasks that will be executed on certain events
  • A typical event is someone trying to access a port on the server
  • Node.js files must be initiated on the server before having any effect
  • Node.js files have extension “.js”

The Event Loop in Node.js

Event Loop

The Event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible. Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background.

Phases of the Event Loop

The Event Loop consists of the six stages listed below, which are repeated as long as the application’s code needs to be executed:-

  1. Timers — Callbacks scheduled by setTimeout() or setInterval() are executed in this phase.
  2. I/O Callbacks — I/O callbacks deferred to the next loop iteration are executed here.
  3. Waiting / Preparation — Used internally only.
  4. I/O Polling — Retrieves new I/O events.
  5. setImmediate() callback — It invokes setIntermediate() callbacks.
  6. Close events — It handles some close callbacks

When Node.js starts executing your index.js file, or any other application entry point, the Event Loop begins. These six stages combine to form a tick, which is a cycle or loop.

The Event Loop itself executes some stages, but the main tasks for others are passed to the asynchronous C++ APIs

Advantages of using Node.js

  • Open Source
  • Java Script language
  • Node js is fast
  • Asynchronous I/O
  • Active community support
  • Rest API
  • Caching Modules
  • Less Problems with Concurrency
  • Unit Testing
  • Streaming Data

Disadvantages of using Node.js

  • Not effective with large scale applications
  • Inconsistent
  • Not suited for CPU-intensive tasks
  • Poorly supported to relational databases

Famous Apps Built on Node.js

--

--

Sandini Pitawala
Sandini Pitawala

Written by Sandini Pitawala

Software Engineering Undergraduate

No responses yet