In today’s web development world, building high-performance applications is a must. This is where Node.js evolved. Before learning Node.js, it is important to have a brief overview of JavaScript.
JavaScript is defined as the language that is used to make web pages interactive. Node.js is a JavaScript Runtime Environment that allows developers to create server-side applications. Previously, JavaScript was used only for client-side scripting in browsers. With the evolution of Node.js, JavaScript is also used for writing backend logic. In this Node.js tutorial, you will learn everything about Node.js, why it is used, its features, and how to set up Node.js on your system.
What is Node.js?
Node.js is not a programming language or a framework. It is actually a JavaScript Runtime Environment that allows developers to execute JavaScript code on the server (outside of a web browser).
It was created by Ryan Dahl in 2009. He is a person who wants to build lightweight and efficient web applications, and as a result, Node.js evolved. Node.js is built on Google Chrome’s V8 JavaScript engine, which is used for compiling JavaScript code into machine code.
How Node.js Works?
To understand why Node.js is so popular, it’s important to know how it works. Node.js is built on Google Chrome’s V8 JavaScript engine. The JavaScript V8 engine takes your JavaScript code and converts it into machine code so that it becomes easy for computers to execute code quickly. Let us understand the working of Node.js with an example.
Traditional web servers like Apache create a new thread for each new request, which means if 100 users come to a server at the same time, then 100 threads are created. This approach can lead to high memory consumption and slow down the performance of your application. Node.js, on the other hand, works differently – it uses a single-threaded, event-driven architecture. When any request comes to the server, Node.js immediately adds it to the event queue. If the request involves simple processing (like reading information from memory), Node.js handles it immediately. But if the request involves a complex task like I/O operations, then Node.js sends the task to the operating system and moves to the next task.
Why Node.js?
- Node.js is used for creating servers. It is a very popular runtime environment used by companies and developers. Here are some of the reasons why Node.js is so popular and used by developers:
- Node.js is built on the V8 JavaScript engine, which compiles JavaScript code into machine code. As a result, Node.js executes code quickly.
- Traditional servers block a thread while waiting for other I/O operations. But Node.js uses non-blocking, asynchronous I/O, which means it can handle other tasks in parallel while waiting for previous input-output operations to complete.
- Node.js comes with more than 1 million packages that you can download easily by using Node Package Manager (npm).
- Node.js has an active community, you can get solutions easily if you face any problem.
- Node.js is cross-platform, which means it runs on Windows, Linux, Unix, and macOS.
Launch Your Web Development Career
Enroll Now
Architecture of NodeJS
- The foundation of Node.JS is the single-threaded event loop model.
- The JavaScript Event Loop Model serves as the foundation for the majority of its processing.
- To better understand the Node.js architecture, let’s go through the following two scenarios:
Situation #1
- The client sends a request to the server.
- The event loop receives the request and records it.
- The node can carry out non-blocking input-output operations thanks to the Single Thread Event Loop.
- To determine whether the incoming request involves any database interaction or complex computations, it is first examined.
- The aforementioned request is prepared because it doesn’t require any complicated operations, and the client is then sent the response.
Scenario #1 Diagram Explanation
- In Case 1, the client contacts the server with request (a).
- The Event Loop contains a record for Request(a).
- The Event Loop then determines whether the request(a) contains any complex operations or database interactions.
- Since the incoming request(a) does not involve any complex calculations, Node js immediately begins processing it, prepares the response, and sends it back to the client.
Situation #2
- The client contacts the server with the request.
- The client submits the request to the server, as can be seen above.
- The Event Loop records the incoming request.
- Following that, the Event Loop determines whether the incoming request involves any sophisticated operations, database queries, or file system interactions.
- The Event Loop consults the Internal Thread Pool, a feature of the Nodejs Server, to determine whether the incoming request involves database interaction.

- The Nodejs Server’s internal thread pool is made up of threads that are in charge of processing requests that involve communication with the file system or a database.
- A thread, which is also referred to as a lightweight process, is a path of execution within a process.
- The Event Loop checks the Internal Thread Pool thread’s availability to carry out the request.
- The request is given to the thread if it is available.
- The assigned thread is then in charge of processing the request, creating a response, and providing the Event Loop with it.
- Finally, the response is returned to the client by the Event Loop.
Scenario #2’s Diagram Explanation
- In Case 2, the client communicates request(b) to the server.
- The Event Loop has Recorded Request (b).
- The Event Loop then determines whether the request (b) involves interacting with a database or requires performing any complex operations.
- Because the incoming request(b) involves interaction with the database and involves heavy computations, Node.js checks to see if there are any threads in the Internal Thread Pool that are free.
- Request (b) is assigned to the thread as soon as it is determined that it is available to handle request(b).
- The task of processing the request, composing the response, and sending it back to the client is then left to the thread.
Core Features of Node.js
Node.js has several amazing features that can make it a great choice to write server-side code. Here is the list of all the important and core features of Node.js:
- Most APIs in Node.js are asynchronous, which means requests are executed in parallel without blocking other requests.
- Node.js uses a single-threaded model with event looping.
- Node.js uses the V8 JavaScript engine, which helps Node.js to execute code quickly.
- Node.js applications do not buffer data because they process data in chunks.
- Node.js is a cross-platform tool, which means it can run on Windows, Linux, Unix, macOS, and more.
- It is open source, which means it is available for all users.
- Node.js has one of the largest developer community support.
Node Package Manager
NPM stands for Node Package Manager, a key reason why Node.js is so popular. NPM is a default package manager for node.js. It is an online repository (or folder) of thousands of open-source Node.js libraries and packages. In other words, you can define it as a package manager tool that allows you to install, manage, and share packages easily in your projects.
To install npm, you have to first install Node.js, because npm comes bundled with Node.js. After installing npm, you can check the version of npm by typing the following command in the command prompt
npm -v
Here is the list of basic npm commands that are used by developers to write server-side scripts:
Commands |
Description |
npm init |
Initialize a new project. It creates a package.json file and keeps track of your projects. |
npm install express |
It installs the Express framework. |
npm install -g nodemon |
-g here means globally. It installs the package globally so that you can run it from anywhere. |
npm uninstall express |
Used for uninstalling packages. |
npm list |
It is used to list installed packages. |
Modules in Node.js
Modules in Node.js are defined as a reusable block of code that contains some pre-written functions, which you can use multiple times in your code. There are mainly 3 types of modules in Node.js:
1. Built-in Modules
Built-in modules are the modules that are included with the Node.js installation. These modules help you perform various tasks without installing any additional libraries. Here is the list of some built-in modules:
Common Built-in Modules |
Description |
fs (file system) |
It is the module that is used to interact with files on your machine. It allows you to perform read and write operations on a file. |
http |
It is used to create HTTP servers and handle HTTP requests or responses. |
path |
It helps you work with file and directory paths. |
os |
It provides information about your operating system. |
url |
It splits the URL address into a readable form. |
2. Local Modules
These modules are created by developers themselves in order to organize their code. It is created in one place and used in other parts of an application.
3. Third-Party Modules
These modules are created by the community and are available for all through Node Package Manager (npm). Jsonwebtoken, bcryptjs, and multer are some examples of third-party modules.
Error Handling in Node.js
Errors are the mistakes in the code that can disturb the actual flow of your program. Handling errors properly in your code is important, and Node.js provides several ways to handle errors effectively. Handling errors properly in your code ensures that your app doesn’t crash while in use. Let us discuss each technique one by one:
1. Using try…catch blocks
The try…catch blocks are one of the simplest ways to handle errors in Node.js. The important point is, it should only work with synchronous code (lines of code executed one by one)
Syntax:
try {
let result = addfunction();
console.log(result);
} catch (error) {
console.error(error.message);
}
The try block contains the risky code, which means the code that may produce some errors. While a catch block contains the solution to that problem. JavaScript only allows a single catch block per try, but you can handle different error types using conditional checks inside it.
2. Using async/await with try…catch
The async/await are two popular keywords in Node.js that help in managing tasks that take time. Here is an example to show how to use async/await with a try-catch block.
Example:
Get 100% Hike!
Master Most in Demand Skills Now!
Applications of NodeJS
IoT applications typically include multiple sensors because small data packets are frequently sent by IoT devices, which can lead to a large number of requests.
Node.js is a wise choice because it can quickly manage these concurrent requests.
Netflix and other companies use Node.js for streaming.
Node.js’s lightweight and quick nature, along with the fact that it offers a native streaming API application, are primarily to blame for this.
Users can pipe requests through these streams to stream data directly to their intended destinations.
Due to its single-threaded asynchronous structure, Node.js is well suited to handling real-time communication.
It is frequently used to build chatbots and is simple to scale.
Additional chat features like multi-person chat and group chat are also easily developed using Node.js.
Advantages of NodeJS
Among the many benefits, some are:
- It is both scalable and lightweight.
- Lowers the amount of code needed to deliver high performance.
- The non-blocking input-output module addresses performance issues because it uses asynchronous programming.
- It is simpler to use thanks to Node.js modules, which resemble a collection of javascript files.
- It is very effective because it can handle numerous requests.
Installing Node.js on Your System
Starting with Node.js is very simple, you just need to install it on your system. Here are the steps to install Node.js on Windows, macOS, or Linux.
Downloading Node.js for Windows
Here are the steps to download and install Node.js on Windows:
- First, go to the official Node.js website.
- You will see two versions there, one is LTS (long-term support) and the other is Current.
- Choose the LTS version that is best for building production applications.
- Download the .msi installer from the Node.js website.
- Run the installer and install it.
- After installing, execute the following commands to check that both npm and Node.js are installed correctly or not.
node -v
npm -v
Downloading Node.js for MacOS
For installing Node.js on macOS, you have to go to the official website and then follow these steps:
The installer will place Node.js and npm automatically on your system.
Download the .pkg installer from the Node.js website
Run the installer and follow instructions properly.
Setting up a Server Using Node.js
First, ensure that you have Node.js installed on your system. After installing Node.js, you’re able to set up or create your first server. Previously, you created a server using Express, but this time, we will use the built-in HTTP module to create a server.
Example:
To execute this code, save this file with the extension .js (server.js). Open your terminal and navigate to the folder where the server.js file is present. And run the following command:
node server.js
After this, open your browser and go to the URL http://localhost:3000
Conclusion
Node.js is an important tool that helps you run JavaScript on the server side. It helps you to create fast, efficient, and scalable web applications like chat apps and streaming platforms. Big companies like Netflix, PayPal, and NASA trust Node.js for creating their apps. If you already know JavaScript, learning Node.js is good for building full-stack applications.
What is NodeJS – FAQs
Q1. Why is Node.js needed?
Node.js is important because it allows developers to build fast and efficient web applications using JavaScript. It is used to write server-side code. It handles multiple server requests without blocking others.
Q2. What is Node.js mostly used for?
Node.js is mostly used to create web servers, APIs, real-time chat apps, streaming applications, and editing tools.
Q3. What is the full form of MERN?
MERN stands for MongoDB, Express.js, React.js, and Node.js. It is a popular stack used to build full-stack applications.
Q4. What is an npm module?
NPM stands for Node Package Manager. It is used to install, manage, and share packages easily in your projects.
Q5. Is NodeJS full-stack?
Node.js is not a full-stack framework, but it plays an important role in full-stack development because it is used to write server-side code.