First we need to understand what is NodeJs??
** How NodeJs Works?? **
First we need to understand what is NodeJs??
Javascript is Browser Type Language . It only runs inside Browser due to Js engine present inside browser which allows to run javascript code on browser console.There are different type of JS engine for every browser.
For Ex- Chrome contain V8 JS engine. Firefox contain spider monkey JS Engine. Safari contain Apple Js Engine.
It is not possible to run Js code Outside the Browser as Js Engine which run JS code is present inside Browser.
Ryan Dahl one of the very famous software engineer and original developer of node.js. He embedded Js Engine inside C++ EngineSo that we can Easily run JS Code outside the browser. Now, we can do File Handling with JS Code due to C++.
Ryan Dahl Named this project as Node.js which is open source project.It is neither a library nor Framework it is Javascript RunTime Environmwnt.
Benefits of Doing This are as Follows
We can easily run Js code outside the browser.
Js can talk to native machine because of c++.
You can create Web Servers in Javascript Language.
Npm in Node.JS
NPM stands for Node Package Manager. when we install node.js it get automatically installed . when we are making any new peoject we have to make one command in terminal such as
npm init // init stands for intialization
It will create package.json File. .It records important metadata about a project which is required before publishing to NPM, and also defines functional attributes of a project that npm uses to install dependencies, run scripts, and identify the entry point to our package.
Now, we will understand How Node.JS Architecture Works:-
In this above Image as shown the client is sending request to web server to interact with web application . The request can be blocking or non- blocking.
Node.js retrieve the Request and add it to Event Queue. For Ex- User 1 make request to Node.js Server it will add to Event Queue. User 2 make request to Node.js Server it will also add into Event Queue. User 3 make request to Node.js Server it will also add into Event Queue. Note that these request will add line by line and the operation perform on these request in the FIFO form. User which requested first will be performed first and so on.
These Request will be sent From Event Queue to Event Loop.
Now, here the Ques Arises What is Event Loop??
So, First we need to understand what is Event Loop- it is type of loop which always having an eye on Event queue. it will check is there any request come in Event Queue or not. If any request came in Event Queue it will take that request but it will work on the principle of FIFO . The request which comes first it will work on that first and the request which comes at last it will work on that in the end. so, from the above points we can understand the working of Event loop that it will catch the request from event queue and work on that and send the response immediately .
Note when Event Loop is picking up the request it will check the request type:-
There are two types of Request:-
Blocking Request
Non-Blocking Request
Blocking Request is also called Synchronous Request and non- blocking request is also called Asynchronous Request.
If the operation is** Non-Blocking operation **it will immediately process it and send the response to the user.
From the above concept what will learn let’s recall it--
First Client send request to web server.
Request will be added in Event Queue
Event Loop will check if there is any request in Event Queue if there is any then it will check the request is blocking or non-blocking
If request is non-blocking then it will immediately process and send the response to the user.
If the Request is Blocking Operation then what we have to do??
Request is Blocking Operation
If Request is Blocking Operation we will go to Thread Pool
What is Thread??
Thread is kind of worker who is working for us. Thread is responsible for working on our Blocking Request.
Blocking Operation will send request to Thread pool . thread pool will check if there is any thread available. As there are limited thread in thread pool.
If thread is free it will send request to thread and when request is performed it will send back the result.
Note there will be only 4 threads available in Thread pool.
Let us assume that more than 4 Blocking Request came in thread pool. it means all the thread become busy . it means 5 user has to wait for processing their request. when one of the thread become free then there request will be proceeded. Let us assume if you are working on a server and there are more than 4 blocking request it means your client has to wait which means waiting time will increased.
So, it is always better to avoid Blocking operation.
Let see the example of Blocking or Synchronous Operation .
Blocking or synchronous image
Output of synchronous operation
Blocking Operation
In the above output of Blocking or synchronous operation we can see that sequence of execution is from top to bottom and 2 and 3 cannot be executed till result cannot be executed . it means synchronous operation blocked the execution of other till the readFileSync take place.
Let see the example of Blocking or Asynchronous Operation .
In the above output of Non-Blocking or Asynchronous operation we can see that sequence of execution is Asynchrnous as 2 and 3 executed before result can be executed . it means Asynchronous operation Cannot blocked the execution of other till the readFileExecution can take place.
— — — — — — — — — — — — — — — — — — — — — — — — — — →
Top comments (0)