September 30, 2022

NodeJs Architecture Part-1

NodeJs Base Architecture

Hello Devs, here i am writing my first article on Nodejs. Sharing my knowledge uptill now. According to docs Node is “Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Perfect for data-intensive real-time applications that run across distributed devices.” also we will be discussing about NodeJs Architecture

I have heard about Node in previous years, and after understanding what all new areas of development it gives me. Node basically communicates with OS directly (thanks to C++ for this ), and here’s the list of libraries node uses.

its an open-source that is built on Chrome’s V8 engine that runs on Linux, Mac OS X, and Windows operating systems. It was first released in 2009 by Ryan Dahl who was one of its original contributors (with some help from Douglas Crockford).

NodeJs Architecture
standard diagram of node’s architecture
  • node waits for instructions to come from javascript environment.
  • node calls/initiate appropriate c++ features
  • node returns the instance of called/initiated c++ feature into javascript environment.

now lets practically see working of above points

const http = require("http");
//port at which the server will run
const port = 4000;
const onIncomingRequest = (request, response) => {
  //send 'Hi, from Node server' to client
  response.end("Hi, from Node server");
};
// giving instructions to NODE to create a server from JS env
const app = http.createServer(onIncomingRequest);
// through createServer() we initialize an internal c++ label to communicate with 
// computer's internals systems, later the same function returns
// an object with methods (in this case const "app" ).
//  now through "app" we can control server's this instance 

//start server and listen for the request
app.listen(port, () =>
  //a callback that will be called as soon as server start listening
  console.log(`server is listening at http://localhost:${port}`)
);

These are the core things that node does. It’s the event loop which handles that take place between the nodejs and javascript. Node event loop handles how the functions that are to be executed. Which internally affects on javascript code. other features like talking to low level mechanism is handle c++ features.

Node Js Event Loop

NodeJS has its own event-loop implemented in that comes from libuv (find more on libuv.org). The reason behind having its own event-loop is simply because in nodejs there are different events, such as “data”, “error” etc, events most of these events do come I/O section that is either we are network request (incoming request) some event is emitted or while reading data from a file a different event is emitted.

The event loop which is usually present in browsers works/considers/understands different events such as “click”, “onBlur”, “onFocus” also the queues present are different then what are present in node.

Node Js Queues

Talking about queues there are 3-4 different queues present in node they are as follows

  • Timer Queue
  • I/O Queue
  • Check Queue

Timer Queue

This queue gets populated when either setTimeOut or setInterval is called. Javascript does not have timers, and all the task related to timers is handled by timers api that is setTimeOut and setInterval.

setTimeout(() => {console.log("timer initiated")}
, 1000);

I/O Queue

This queue has callbacks from all I/O related events such as incoming/outgoing network requests, incoming data from file . Here javascript tells node what to do node handles that in background and after node has completed its job, the appropriate callbacks gets added to the queue for eg: (javascript tells node to bring data from a file ‘temp.json’). While node is handling that part js goes to below program and executes and when node has done its job the appropriate callbacks gets registered into the node.

Check Queue

this queue has the lowest priority as i want this code after all I/O operations are carried out. To populate this queue 1 can use setImmediate, for eg

setImmediate(()=>{console.log(' at last of the queues')})

In the next part we’ll be studying about microTask, macroTask and process.nextTick() and more details about the node js event loop.