79799839

Date: 2025-10-26 04:54:24
Score: 0.5
Natty:
Report link

Nodejs architecture

TL;DR: https://github.com/nodejs/node/

I would say there are 3 main components in the concept:

  1. V8 Engine

  2. libuv

  3. nodejs core (I named it as orchestrator)

When you running cmd like: node index.js what happens?

First, how do you use node command? There is a daemon server (server run in background) provide CLI right - this is node daemon server.

Node run StartExecution entry and doing stuff:

  1. Nodejs start V8 sandbox with a Just In Time (JIT) Compiler - incharge of compile js code to machine code based on ECMAScript and WebAssembly standard.

  2. Node start internal binding - wrap external dependecies call to C libraries like file system, stream, ... to Node API like node:fs, node:stream, ...

  3. Node start libuv event loop (THIS IS WHAT YOU USALLY HEARD) to manage asynchronous tasks. We need to pay attention to:

    1. Worker Thread Pool

    2. Callback Queue

Okay, finish the startup, now the entry file is index.js, node will say: "Hey V8, please run this JS code"

V8 compile JIT and run code line by line, if it s just native JS code like: const a = 1 + 2 will be execute directly and for function call like function sum() {} it wil put into call stack to execute.

For async tasks (micro tasks, macro tasks) like: Promise, timers, ... Now is the heart of nodejs where the game start.

When you try to run something like: fs.readFile('./file.pdf', 'utf-8', (content) => {...});

Remember when I said Node do internal binding , node delegate the call to external deps of C library AND managed by Async Tasks Manager of Libuv.

  1. At the Nodejs API level, it verifies parameters like path, Encoding, ... data types, and now it call to the binding of nodejs core API, it register to the QUEUE:

    1. the task

    2. the callback

  2. Worker Thread from libuv pickup the task, execute the call using C library, locking the thread resource if it is I/O task, if OS task, it handover to OS Async

  3. When task finished, it put the result into the CALLBACK QUEUE

  4. The libuv Event Loop see that we have the result, let s trigger the callback above, this callback is actually a C pointer reference to the callback in JS code that we registered.

At the end, the JS code have no code to execute anymore, UV Loop Empty, no tasks left, so nodejs will start ending the running process and emit the signal to system to stop this.

Reasons:
  • Blacklisted phrase (1): how do you
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Justin Dang