Node.js is an open-source, cross-platform JavaScript runtime environment built on Google Chrome's V8 JavaScript engine. Node.js executes JavaScript outside the browser, enabling developers to build scalable, high-performance network applications, microservices, and backend APIs using a single programming language across the entire stack.
Traditional server environments (like Apache with PHP or Java servlets) historically spawned a new operating system thread for each incoming client request. A server handling 10,000 concurrent requests would require 10,000 threads, quickly exhausting server RAM and spending tremendous CPU cycles on thread context switching.
In contrast, Node.js operates on a single-threaded, non-blocking, event-driven architecture:
The Event Loop is the heart of Node.js. It continuously monitors the Call Stack and task queues, orchestrating when asynchronous callbacks are pushed to the stack for execution.
The Event Loop cycles through several distinct phases in each "tick":
| Phase | Responsibility | Example Tasks |
|---|---|---|
| Timers | Executes callbacks scheduled by setTimeout() and setInterval() | Delayed notifications, interval timers |
| Pending Callbacks | Executes I/O callbacks deferred to the next loop iteration | System-level network socket error callbacks |
| Idle, Prepare | Used internally by Node.js for diagnostics and runtime housekeeping | Internal Libuv operations |
| Poll | Retrieves new I/O events and executes their callbacks | Incoming HTTP requests, file read buffers |
| Check | Executes callbacks registered by setImmediate() | Code explicitly deferred until after poll phase |
| Close Callbacks | Handles resource cleanup and teardown | socket.on('close', ...) |
Output:
The main thread never waits for disk or network I/O. It delegates the I/O to Libuv and immediately continues processing subsequent synchronous statements.
setImmediate() executed?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| None | Call Stack, Callback Queue, Microtask Queue & Libuv Workers |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.