Internal Architecture Of NodeJS

Posted By : Ankit Uniyal | 30-Sep-2018

The goal of this blog is to understand the Internals of Node.js ecosystem, How Node.js works behind the scenes.

Now, we're going to start looking at some of the internals of Node.js and once we have a better idea of Nodejs internal working then we will able to use that knowledge so that we can write some performant code in our applications.

Now, we're going to start off by taking a look at a very simple diagram.

 

 

 

At the very top, we've got a box that represents the javascript code that you we write i.e this is the actual code that we place into javascript files and then eventually it executed at the command line.

When we run and then index.js at the command line, we are invoking nodejs project. Just like any JavaScript project Nodejs has the collection of dependencies that it actually uses to execute our code.


Now, we are going to focus on two of the most important dependencies which areV8 project and the libuv project.


The V8 project is an open source javascript engine which created by Google as it executes the javascript code outside the browser and exactly what we do whenever we run our javascript code from the terminal.


Whereas the libuv project is a C++ open source project which gives node access to the operating systems underlying the file system. It provides access to networking with some access to concurrency as well.


Now, we strike with an interesting question, so we got one module that gives access to the file system and networking and other stuff and then another which executes our javascript code.

 

Also read: Why Is NodeJS More Crucial For Your Business Than You Think

 

So what is the purpose of Nodejs?

 

Why we use Node at all and why we don't directly use V8 or libuv.

 

The first thing to understand is that internally some of these other libraries are not javascript code at all.

The V8 project is like 70% C++ code whereas libuv is 100% C++ code, so there might be chances that as a javascript developer you probably don't want to be writing C++ code at all.


Also, the other thing that node provides to us is a series of wrappers and a very unified inconsistent API for us to use inside our projects.

Node implemented HTTP, fs, path and crypto modules and all these modules have consistent API's.


Since they all ultimately refer to the functionality that is mostly written inside of the lib project as you don't want to get access to direct C++ code and you want to require some javascript function and use a javascript function inside of your code.


The Node.js Event Loop:


 

The Event loop is used by the node to handle asynchronous code that we write inside our applications. 

Now, before going straight into the event loop, however,  we're going to discuss something called threads, once we understand what a thread is then we will go back to the event loop.


Whenever we run programs

in our computer, we initiate something which called as a process. A process is an instance of a computer program that has been executed.

Within a single process, we can have multiple things called threads. A thread is a to-do list that has some number of instructions that need to be executed by the CPU of your computer.

 

Read more: Introduction of Environment file In Angular2

 

Now we have some basic understanding of what is a thread all about, so let's get back to what is Event Loop?

 

You can think of the event loop as being like a control structure that decides what our one thread should be doing at any given point in time.

The event loop is the absolute core of every program that we run and every program has exactly one event loop. As a lot of performance concerns about node boiled down to eventually how the event loop behaves.

 

Event loop allows node.js to execute non-blocking operations. It examines the HTTP request, is blocking or non-blocking also check the call stack i.e is it empty or not. Whenever event loop finds a blocking HTTP request, it passed the request to the thread pool along with a message else it will execute and send the response back to the caller.


Hope it helps you out. Keep Learning.

Thanks.

About Author

Author Image
Ankit Uniyal

Ankit has knowledge in Javascript, NodeJS, AngularJS and MongoDB also have experience in using AWS Services.

Request for Proposal

Name is required

Comment is required

Sending message..