Node.js Architecture
Node.js uses an event-driven, non-blocking I/O architecture. This helps it handle multiple requests efficiently without creating a separate JavaScript execution thread for every request.
1. What Is Node.js Architecture?
Architecture means the internal structure of a system and how its different components work together.
Node.js architecture explains how JavaScript code is executed, how input/output operations are handled, and how completed operations return their results to the program.
For example, when Samadhan requests a file from a Node.js server, Node.js can start reading the file without blocking other JavaScript work while the file operation is waiting to finish.
2. Main Components of Node.js
The following components work together to execute JavaScript and handle asynchronous operations.
| Component | Simple Explanation | Responsibility |
|---|---|---|
| V8 Engine | Executes JavaScript code. | Compiles and runs JavaScript. |
| Event Loop | Coordinates pending callbacks. | Helps Node.js process asynchronous work. |
| libuv | A library used by Node.js. | Provides the event loop and asynchronous I/O support. |
| Node.js APIs | Features available to JavaScript programs. | Provide access to files, networking, timers, and more. |
| Operating System | The system on which Node.js runs. | Provides facilities for networking, files, and other operations. |
3. Node.js Architecture Diagram
This simplified diagram shows how a JavaScript application interacts with Node.js and its supporting components.
JavaScript Application
Application code
Node.js Runtime APIs
File System, HTTP, Timers, and other APIs
V8 Engine
Executes JavaScript code
libuv
Event Loop and asynchronous I/O support
Operating System
Networking, file operations, and other system services
Note: This is a simplified learning diagram. The actual internal architecture has additional components and interactions.
4. What Is the V8 Engine?
V8 is the JavaScript engine developed by Google. Node.js uses V8 to execute JavaScript code.
When Pradnya writes JavaScript statements, V8 processes the code and executes it. V8 also uses compilation and optimization techniques to improve execution performance.
Example
let a = 10;
let b = 20;
console.log(a + b);
Output
30
Explanation:
V8 executes the JavaScript code. The addition operation
produces 30, and console.log() displays it.
5. What Is libuv?
libuv is a library used by Node.js to support asynchronous operations and the Event Loop.
It helps Node.js handle operations such as file-system work, timers, and networking. Depending on the operation, libuv can use operating-system facilities or its worker pool.
For example, Samadhan can request that a file be read. Instead of waiting synchronously for the entire operation, he can use an asynchronous file API and handle the result in a callback.
6. What Is the Event Loop?
The Event Loop helps Node.js coordinate asynchronous callbacks. It allows JavaScript to continue executing other work while certain operations are waiting to complete.
Suppose Pradnya starts a timer for two seconds. Node.js does not need to stop all JavaScript execution while the timer is waiting. Other statements can run first.
Event Loop Diagram
For example, a timer or file read
After the operation is ready and its callback can execute
7. Synchronous vs Asynchronous Operations
| Synchronous | Asynchronous |
|---|---|
| Waits for an operation to finish before continuing. | Allows other work to continue while supported operations are pending. |
| Can block the JavaScript thread. | Can avoid blocking the JavaScript thread while waiting. |
Example: readFileSync(). |
Example: readFile(). |
Asynchronous does not mean that every operation executes simultaneously. It means the program can coordinate work without waiting synchronously for each operation.
8. Practical Program: Understanding the Event Loop
Samadhan wants to understand why a message outside a timer can appear before the timer's callback.
Step 1: Create a File
Create a file named eventloop.js.
Step 2: Write the Program
console.log("Program started");
setTimeout(() => {
console.log("Timer completed");
}, 2000);
console.log("Program continues");
Step 3: Run the Program
node eventloop.js
Step 4: Expected Output
Program started
Program continues
Timer completed
Step 5: Understand the Output
-
Program startedprints first because it is the first statement. -
setTimeout()schedules a callback after a delay of at least approximately two seconds. -
JavaScript continues and prints
Program continues. -
When the timer callback becomes eligible to run, Node.js executes it
and prints
Timer completed.
9. What Is Non-Blocking I/O?
I/O means Input/Output. Examples include reading files, receiving network requests, and sending data.
In non-blocking programming, Node.js can start an asynchronous operation and continue with other JavaScript work rather than waiting synchronously for that operation to finish.
Example: Reading a File
First, create a file named message.txt and write:
Hello from Samadhan!
Now create fileexample.js in the same folder.
const fs = require("node:fs");
console.log("Reading file...");
fs.readFile("message.txt", "utf8", (err, data) => {
if (err) {
console.log("Error reading file");
return;
}
console.log(data);
});
console.log("Other work continues");
Run the Program
node fileexample.js
Typical Output
Reading file...
Other work continues
Hello from Samadhan!
Explanation:
fs.readFile() starts an asynchronous file read.
The next statement can run before the file-read callback.
Once the file contents are available, the callback handles the result.
10. Does Node.js Use Only One Thread?
Node.js usually executes JavaScript callbacks on one main JavaScript thread per process. However, that does not mean the whole runtime uses only one thread.
- JavaScript callbacks generally run on the main JavaScript thread.
- Some file-system, DNS, and other operations can use libuv's worker pool.
- Networking often relies on operating-system asynchronous facilities.
- Node.js can also create worker threads or child processes when needed.
This architecture is useful for applications that handle many I/O operations. CPU-heavy JavaScript can still block the main JavaScript thread if it runs for a long time.
Important Points
- V8 executes JavaScript code.
- libuv supports the Event Loop and asynchronous I/O.
- The Event Loop coordinates scheduled callbacks.
- Non-blocking I/O allows other work to continue while an operation is pending.
- A timer callback does not necessarily run at its exact requested time.
- CPU-heavy JavaScript can block the main JavaScript thread.
Summary
Node.js combines the V8 JavaScript engine with runtime APIs and supporting components such as libuv. Its event-driven, non-blocking I/O model helps applications handle asynchronous work efficiently. We also used setTimeout() and fs.readFile() to understand how JavaScript can continue working while an operation is pending.