NodeJS Web Server architecture

Arslan Ali
Nerd For Tech
Published in
3 min readMay 19, 2021

Node is a run time environment to execute JavaScript code.

Wait… What is a run-time environment? So, we need to go back a few years ago to JavaScript only to build applications that run inside of a browser. In every browser, there is a JavaScript engine that takes the JavaScript code and converts it into machine code. For example, Microsoft edge uses the Chakra, Firefox uses Spider Monkey and chrome uses V8. Due to these engines sometimes javascript code behave differently in other browsers. Here browser provides us the runtime environment for the javascript code. For example, we have document.getElementById(“#id”), we can use the document object in runtime to perform different operations. That’s why before 2009 javascript code was only executable in browsers.

In 2009 a developer at google take the v8 engine of chrome and put this inside a c++ program and called it a node. So similar to the browser, Node is just a runtime environment for executing the javascript code. It contains the javascript engine same as the browser and it also has some environment objects. These objects are different from the browser objects like document object is not available in node, instead, we had different objects like “fs” for file system tasks, ‘’http’’ for network tasks, and so on.

NodeJS Server Architecture:

NodeJS uses the “Single Threaded Event Loop” architecture to handle multiple concurrent client requests. When a request comes to the Node server it works like an event-driven processing model of javascript with a callback mechanism. There are different parts in NodeJs Server.

NodeJS Web Server Architecture Diagram. In this diagram, I had explained about the node js server architecture working
NodeJS Web Server Architecture
  1. Requests:

The request is coming from the client side and the user wants a response from the server to act. These actions may be non-blocking simple or they may be blocking complex tasks.

2. NodeJS Server:

NodeJS is used on the server-side. It takes the incoming client request from the users, processes the requests, and responds to that particular client or user.

3. Event Queue:

In NodeJS event queue stores the request and passes them one by one into the event loop.

4. Event Loop:

The event loop receives the request from the event queue, processes the request, and returns the response to the particular client.

5. Thread Pool:

The thread pool consists of the available threads in our NodeJS server which will take some tasks to complete the client request.

6. External Resources:

External resources are those which need to fulfill the blocking operations in client requests. These resources can be for computation, data storage, etc.

Workflow of NodeJS Architecture:

NodeJS web server will receive the requests from the clients, these requests may contain the operations which are blocking or non-blocking. The server will retrieve these requests and add them to the event queue.

NodeJS Web Server Architecture Diagram. In this diagram, I had explained about the node js server architecture working
Node JS Web Server architecture workflow

In the event queue, these requests will be processed one by one and pass to the event loop. The event loop will check the request is contains any blocking operations or it’s a simple request.

If the request is blocking then it will be passed to the thread pool otherwise it’s will process the simple request like I/O polling and returns the response to the client.

A single thread from the thread pool is assigned to a single complex request. This thread will be responsible to complete that particular blocking operation by using external resources such as compute, database, file system, etc. Once the task will complete it will pass the response to the event loop then the event loop will send the response to the client.

Summary:

NodeJS is runtime to execute JavaScript program other than browser.

NodeJS consists of some of the extra global objects then browser.

NodeJS is by default asynchronous and handles thousands of requests. That makes it more efficient.

--

--

Arslan Ali
Nerd For Tech

Software Engineer at Babbel GmbH. Experienced with Typescirpt, Golang, Javascript, Serverless, NodeJS, AWS & React.