NodeJS处理客户端传入请求的流程解析及核心疑问解答
Hey there! I totally get where you’re coming from—this stuff can feel opaque at first, even after reading a bunch of articles. Let’s break this down step by step, starting with clarifying your existing understanding and then answering your questions.
First: Clarifying Your Current Understanding
Your core idea is on the right track, but let’s add some precision:
- Node.js doesn’t just "listen" on a port by itself—it relies on the libuv library (the C++ layer that handles async I/O for Node.js) to set up a TCP socket listener.
- When a client (like React or Postman) sends a request, it first establishes a TCP connection with your Node.js server. Once the connection is up, the client sends HTTP data.
- When libuv finishes receiving and parsing the HTTP request data, it adds the request handling callback (the logic you wrote, like
(req, res) => { ... }in a basic HTTP server) to the poll phase queue of the event loop—not the raw request itself. - The event loop will pick up this callback during its poll phase when the execution stack is empty.
Answering Your Questions
1. When does Node.js do route matching in the event loop?
Route matching (whether you’re using the native http module or a framework like Express/Koa) doesn’t happen in a specific event loop phase—it happens when your JavaScript request-handling code runs in the V8 engine.
Here’s how it works:
- When the event loop picks up the request callback from the poll queue, it pushes this callback onto V8’s execution stack.
- Your route matching logic (e.g., checking
req.urlin the native module, or Express’sapp.get('/api', ...)routes) is part of this callback execution. V8 runs this synchronous JS code to match the request to the right handler.
In short: Route matching is part of the JavaScript code execution step, which the event loop triggers by pushing the callback to the execution stack.
2. What happens after the event loop picks up the request?
Once the event loop grabs the request-handling callback from the poll queue:
- It pushes the callback into V8’s execution stack, and V8 starts running your JS code.
- If your code has synchronous logic (like route matching, data validation, or simple calculations), V8 runs it immediately until the execution stack is empty.
- If your code hits an async operation (e.g., reading a file, querying a database, or calling a third-party API), Node.js hands this operation off to libuv (or other async providers). Libuv handles the I/O work in the background without blocking the event loop.
- When the async operation finishes, libuv adds its completion callback to the appropriate event loop queue (usually the poll queue, unless you used
setImmediatewhich goes to the check phase). - Once your initial request callback finishes and the execution stack is empty, the event loop moves to the next phase (like check, close callbacks) and continues processing queued tasks.
3. How does the V8 engine process the request?
V8’s role is to execute your JavaScript code—here’s the play-by-play:
- When libuv is ready to trigger the request handler, it passes the
reqandresobjects (these are hybrid C++/JS objects that bridge Node.js’s native layer and V8) to the callback function. - V8 parses, compiles, and executes the callback function in its execution stack. This includes all your request-handling logic: route matching, processing request data, generating responses, etc.
- For any async calls in your code, V8 invokes Node.js’s native bindings to pass the task to libuv. V8 then continues executing the rest of your synchronous code until the stack is empty.
- When libuv signals that an async task is done, the corresponding callback is added to the event loop queue. The next time the event loop reaches the right phase, it pushes this callback to V8’s execution stack to run.
内容的提问来源于stack exchange,提问作者0x550x42

