Sitemap
Saurav sharma

Saurav sharma

Full stack developer

How NodeJs execute code internally

2 min readFeb 6, 2025

--

Press enter or click to view image in full size

When a client sends a request to a Node.js server, many processes work behind the scenes to handle and respond to it efficiently. Let’s break down these steps in detail.

1. Event Queue

The Event Queue is a queue that holds all the operations assigned to Node.js. Tasks waiting for execution are placed in this queue before being processed by the Event Loop.

2. Event Loop

The Event Loop is the core mechanism of Node.js that continuously processes tasks from the Event Queue. However, it needs to differentiate between two types of tasks:

  • Blocking (Synchronous) Tasks
  • Non-blocking (Asynchronous) Tasks

Since JavaScript and Node.js both are single-threaded, blocking tasks can halt execution, reducing efficiency. To handle this, Node.js delegates non-blocking tasks to a worker thread pool, managed by libuv.

3. Thread Pool & Non-Blocking Execution

The libuv thread pool consists of multiple worker threads that handle asynchronous operations such as:

  • File system operations (e.g., reading/writing files)
  • Cryptographic functions (e.g., bcrypt hashing)
  • Compression tasks (e.g., zlib operations)
  • Some DNS and networking operations

When a non-blocking task requires a worker thread, it is assigned to an available thread in the pool. Once the task is completed, the result is sent back to the Event Loop, which then returns the response to the client.

Final Thoughts

This is a fundamental overview of how Node.js processes requests internally. In an upcoming article, I will dive even deeper into the internals of Node.js, including how timers, microtasks, and OS-level optimizations work.

Get Saurav sharma’s stories in your inbox

Join Medium for free to get updates from this writer.

I hope this article helps you understand the working of Node.js better. If you found it valuable, please like and support for more such content!

Thanks & Regards,
Saurav Kumar Sharma

--

--