What is Node.js? What is it used for?
— NodeJS — 9 min read
The official website defines Node.js as -
Open-source means its source code is freely available on github and anybody can contribute to its development. Cross-platform means it can be used on any kind of platform like Windows, Linux or MacOS.
To give you some context, there are different runtime environments for different programming languages and frameworks like Java Runtime Environment( JRE ) for JAVA and Common Language Runtime( CLR ) for .NET.
Node.js was created by Ryan Dahl in 2009 with the purpose of creating real-time web applications that could handle large number(10,000+) of user connections.
What is Node.js used for?
Again according to the official website -
Node.js is mainly used for server-side web development that involves creating API services or the "backend" of a web application that primarily performs CRUD operations.
Here are some examples of applications that can be built using Node.js.
For example, the LinkedIn Mobile API uses Nodejs and the Express framework.
For example, NASA chose to implement a microservices architecture using NodeJS and noticed a 300% improvement in database access speed.
For example, Netflix improved its user experience and performance thanks to Node.js.
Real-time messaging and collaboration tools.
For example, eBay.
For example, Paypal.
CMS websites( Content Management System enabled websites)
Command Line Interface tools
The primary difference is that in Node.js, you don't have access to the DOM because there is no DOM like there is in the browser. So you don't have access to
window or any other Web APIs because they simply do not exist.
Node.js uses a single thread or process for running applications and handling requests.
But what happens if a time-consuming I/O operation blocks this main thread? Does that mean that Node.js just sits there waiting for the thread to become available again?
The answer is "No".
Node.js follows a non-blocking paradigm and exposes asynchronous APIs that do not block the main thread.
Node.js also exposes synchronous API functions that could potentially block the main thread but it strongly discourages the use of these APIs.
So when Node.js needs to perform an I/O operation like handling a network request or fetching data from the filesystem or a database, it initiates the operation and instead of waiting for a response, it starts responding to other requests until the response from the first operation becomes available. When the response is available, it resumes the processing of this operation.
This enables Node.js to handle hundreds and thousands of concurrent requests.
Also developers do not have to manage multiple threads and and don't have to deal with concurrency issues. This means fewer bugs and also higher scalability.
Asynchronous I/O using Libuv
Libuv is a multi-platform library written in C that primarily focusses on performing asynchronous I/O which includes reading and writing files and making network requests.
This library is what Node.js uses to perform asynchronous I/O.
Event-Driven Architecture and the Event Loop
Node.js is based on an event-driven architecture. This means that it listens for events and responds to them by executing some code to handle that event.
This is similar to how a browser runs some JS code to handle click events or respond to a successful XHR request.
There are two main activities here:
- Listening for events
- Executing some code when these events happen.
Both of these are achieved using an Event Loop.
The basic idea behind the Event Loop is that the runtime is continuously waiting for events to occur. When an event occurs, the runtime is informed and it runs some code in response.
For example, when Node.js encounters a file read operation, it initiates the operation using it's file I/O API which in turn communicates with Libuv. Since Libuv is involved, this I/O operation is handled asynchronously. Thus Node.js basically delegates or gives away control for that particular I/O operation to Libuv along with instructions on how to inform Node.js when the I/O operation is complete. These instructions are typically given in the form of a callback or a promise.
When the file reading is complete and the file data is available, an event is triggered and Node.js is informed using the instructions that were provided when the operation was initiated.
Since the runtime is always listening for events, it realizes that this event has occurred and that it should execute a callback in response. It adds the callback to a Queue and attends to it in First-In-First-Out manner.
What are modules in Node.js?
We've already seen that out of the box, Node.js provides APIs to interact with the filesystem, make network requests, etc. This functionality along with other features are encapsulated within containers referred to as "modules".
So for example, if we need file reading and writing capabilities, all we need to do is import the file system module or
import fs from 'node:fs';
If we need to handle network requests, we can import the HTTP module or
import http from 'http';
What is NPM( Node Package Manager )?
You are not just limited to the modules that are in-built within Node.js.
Other developers build their own third-party modules to extend the functionality already available within Node.js. For example, the very popular ExpressJS framework.
This is where NPM or the Node Package Manager comes into the picture.
NPM manages all the third-party modules that you include into a Node.js application. NPM is already baked in along with the Node.js installation.
So for example, you can install the
express framework by using this terminal command.
npm install express
You can import this into a Node.js application just like you'd import an in-built Node.js module.
import express from 'express';
All these third-party NPM packages are listed in the NPM registry.
NPM is largely responsible for the huge success of Node.js as developers find it really easy to build Node.js apps thanks to the 350,000+ packages available in the NPM registry.
Is Node.js a programming language?
Is Node.js a framework like ASP.NET?
With a Node.js web app, you can choose to use a framework like ExpressJS for powering your web application.
Is Node.js a web server like Apache or IIS?
But it has the capabilities to spin up web servers so a "Node.js web application" can be said to be a web-server like Apache, Nginx or IIS.
Can I run a Node.js app using IIS or Apache server similar to how I would execute PHP code?
Yes it is possible to host your Node.js app on an IIS or Apache or Nginx server.
Here are some resources to help you with that:
Can I use a Node.js server for my Node.js web app in production?
Yes you can. There are plenty of websites out there that just use the Node.js web server to serve their Node.js web app.
When Node.js was a relatively new technology, a common practice was to put an Apache or Nginx server in front of your Node.js app( with Nginx being the preferred choice ).
This has a couple of advantages -
- Servers like Apache and Nginx are dedicated web server technologies that have been honed and battle-tested to be fast and secure when it comes to handling HTTP requests.
- They also provide options for load balancing and caching proxy. Doing this in Node.js will require you to write code and implement your own load balancers and caching proxies.
So you could consider both options based on your requirements for a web server.
What is express in Node.js?
ExpressJS is a popular Node.js based framework. It forms a layer on top of Node.js and provides a host of features like routing, middlewares, templating, etc that help developers quickly get started with building APIs and web and mobile applications.
Hope this helps!