Now let us understand how NodeJS process the requests by following the below diagram.
From the above diagram, all the requests which are going to NodeJS server will be maintained in “Event Queue“. The “Event Queue” will be processed by “Event Loop” which will run with single thread. The “Event Loop” will take requests from the “Event Queue” and the response will be given back to the client. The “Event Loop” will run infinitely to complete the requests available in the queue. If there are no requests in the queue, the event loop will be in ideal state. For example, the request R1 is a non blocking I/O request, then the event loop will return the response immediately to the client. In the above diagram request R2 is expecting information from external systems like database/file system, then the event loop will leave that request to the “Node Internal Thread Pool”. A thread from the thread pool will take the request R2 to process. Whenever the request R2 is ready with the data, then the thread handover the request R2 to event loop. Then the event loop will send the response back to the respective client. This is how NodeJS server handles multiple requests with “Single Threaded Event Loop“.
In the coming articles we will see NodeJS running environment with examples. Till then, Happy Learning!!!