When http request hit into the Nodejs application the connect first received by Nodejs main thread (which is js main thread) and passed to event loop, if my understand is correct
The above statement is from Nodejs official documentation, so my question where does the main thread lives?
out side the event loop or inside the event loop
is both different? Can someone give good pictures how Nodejs work internally, with graphical representation?
Main thread is the execution thread. As asked, it stays outside the event loop and will check for any events pertaining to the event loop.
If the even added to the event loop is synchronous and only needs some CPU operations, the main thread will take control directly and will execute the need.
If an asynchronous event is added to the event loop that is delegated to the worker threads available. Worker threads will perform the async operation(db/API/fs calls). Once the async part of the event is completed the event is again added to the event loop for the execution of the remaining synchronous part.
The worker threads are built over
libUV and will spawn according to the main thread requirement. This reason makes Node.js more efficient while handling async calls and less preferred for CPU extensive operations.
Follow this for a more detailed explanation:
What function gets put into EventLoop in NodeJs and JS