Backend Development 16 min read

Understanding Node.js: Architecture, Event Loop, and Asynchronous I/O

Node.js is a server‑side JavaScript runtime built on V8 that replaces the browser environment with native libraries, uses libuv’s event loop and thread pool to provide non‑blocking asynchronous I/O, and organizes its architecture into application code, V8, bindings, and libuv layers for efficient backend development.

Baidu Geek Talk
Baidu Geek Talk
Baidu Geek Talk
Understanding Node.js: Architecture, Event Loop, and Asynchronous I/O

Node.js is a server‑side JavaScript runtime built on the V8 engine. It allows developers who are familiar with JavaScript to write backend services without learning a new language such as PHP or Python.

The official definition states that Node.js is a JavaScript runtime environment based on V8. In practice this means that Node.js executes JavaScript code using the same engine as Chrome, but it provides its own set of APIs (e.g., fs , http , process ) for file system access, networking, encryption, and more.

Before Node.js, the most common JavaScript runtime was the browser, which supplies DOM APIs and a rendering engine. Node.js replaces the browser’s host environment with its own, built on V8 plus a collection of native libraries (libuv, c‑ares, llhttp, OpenSSL, zlib, etc.).

Event Loop and Asynchronous I/O

Node.js follows an event‑driven model: when a request arrives, the server registers the event, processes it, and immediately returns to listen for the next request. This non‑blocking approach is powered by libuv, which implements the event loop and a thread pool for I/O operations.

The event loop maintains two queues:

Macrotask queue : timers (setTimeout, setInterval), I/O callbacks, UI rendering, etc.

Microtask queue : Promise callbacks.

Before executing any macrotask, Node.js drains the microtask queue, ensuring that microtasks have higher priority.

Process and Thread Model

Each Node.js process contains a main JavaScript thread and a pool of C/C++ threads managed by libuv. System calls can be blocking or non‑blocking; Node.js prefers non‑blocking calls and uses the thread pool to perform the actual work, pushing the result back to the event loop as a callback.

Blocking vs. Non‑blocking, Synchronous vs. Asynchronous

Blocking refers to the behavior of the callee (the OS system call). Non‑blocking means the call returns immediately and the result is delivered later. Synchronous vs. asynchronous describes the caller’s perspective: a synchronous call waits for the result, while an asynchronous call continues execution and handles the result via a callback.

Node.js Architecture

The stack consists of four layers:

Application code (your JavaScript).

V8 engine (parses and executes JavaScript).

Node.js bindings (expose native libraries to JavaScript).

libuv (provides the event loop, thread pool, and low‑level I/O).

Above these layers sit the Node.js standard library and any C/C++ add‑ons you may compile.

Example: Using the events module

var events = require('events');
var eventEmitter = new events.EventEmitter();

Binding and emitting an event:

eventEmitter.on('eventName', eventHandler);
eventEmitter.emit('eventName');

Full example (main.js):

// 引入 events 模块
var events = require('events');
var eventEmitter = new events.EventEmitter();

// 事件处理函数
var connectHandler = function connected() {
    console.log('连接成功~~~');
    // 触发 data_received 事件
    eventEmitter.emit('data_received');
};

// 绑定 connection 事件
eventEmitter.on('connection', connectHandler);

// 绑定 data_received 事件(匿名函数)
eventEmitter.on('data_received', function () {
    console.log('数据接收完毕。');
});

// 触发 connection 事件
eventEmitter.emit('connection');

console.log('程序执行完毕。');

Running the script produces:

$ node main.js
连接成功~~~
数据接收完毕。
程序执行完毕。

The article also covers the differences between browser and Node.js event loops, the stages of the Node.js loop (timers, pending callbacks, poll, check, close callbacks), and the concepts of blocking I/O, non‑blocking I/O, and polling.

Overall, the piece provides a comprehensive introduction to Node.js fundamentals, suitable for front‑end developers transitioning to backend development or any programmer interested in JavaScript runtime internals.

JavaScriptNode.jsEvent Loopasynchronous I/Olibuv
Baidu Geek Talk
Written by

Baidu Geek Talk

Follow us to discover more Baidu tech insights.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.