AceDevHub
Beginner Node.js Interview QuestionsBeginnerConcept

Node.js · Question 2

How does Node.js work?

Direct answer

Node.js receives JavaScript on the main thread, V8 compiles and runs it, async I/O delegates to libuv (OS or thread pool), and completed work returns via callbacks queued on the event loop — so the main thread stays free while I/O runs in the background.

Picture a request lifecycle: your route handler runs synchronously until it hits async I/O (read file, DB query, HTTP call). Node registers the operation with libuv, continues other work, then runs your callback when data is ready.

  1. Script entry — node app.js; V8 parses and executes top-level code.
  2. Sync work — runs immediately on the call stack until empty.
  3. Async I/O — fs.readFile, net.connect hand off to libuv; callback queued when done.
  4. Event loop — picks callbacks from phase queues when the stack is clear (Q4–5).
  5. Response — handler sends res.json(); loop waits for next request.
request-flow.js
import fs from "node:fs";

console.log("1: start");

fs.readFile("package.json", "utf8", (err, data) => {
  console.log("3: file read", data.length, "bytes");
});

console.log("2: end — main thread free while libuv reads disk");
Output
1: start
2: end — main thread free while libuv reads disk
3: file read 842 bytes