I have been learning and using Node.js for two months. I have used express and combined with mongoose to write a web application and a set of RESTful web APIs. Looking back at the introduction of Node.js on the homepage of the official website of Node.js: Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. So what does non-blocking I/O model mean?
Non-blocking IO model
First of all, IO operations are undoubtedly time-consuming. When the server receives a large number of requests, creating a process or thread for each request also adds additional memory overhead, and may waste more time and resources.
Since Node.js is event-driven, it uses event loops to solve the bottleneck problems caused by IO operations. In Node.js, an IO operation usually has a callback function. When the IO operation completes and returns, the callback function will be called, and the main thread continues to execute the following code. Let's briefly illustrate this problem with an example:
request('http://www.google.com', function(error, response, body) { console.log(body);}); console.log('Done!');This code means to issue a request to 'http://www.google.com', and when the request returns this callback function, the response information is output. Due to the running mechanism of Node.js, after this code is run, it will immediately output 'Done!' on the console, and then output the response information after a period of time.
Event loop event loop
Next, let’s discuss the mechanism of event loop. First, let’s talk about the call, for example, there is the following code:
function A(arg, func){ var a = arg; func(); console.log('A'); } function B(){ console.log('B');} A(0, B);After the code is executed, function A is first pushed into the call and becomes the top element of the stack and starts executing A. During the execution process, function B is pushed into the call and becomes the top element of the stack. After B is executed, B is popped up and called, and A becomes the top element of the stack again. After A is executed, A is popped up and called, and the call is idle.
There is a message queue in the Javascript runtime, and the message is associated with a callback function. When an event is triggered, if the event has a corresponding callback function, the message will be added to the message queue.
Let’s talk about what the event loop is. After the code starts executing, the function is constantly pushed into the call. Take the example above. Request is pushed into the call, and this function will make an http request (this http request will be handed over to the underlying module of Node.js) and the event completed at the same time is associated with a callback function. Request is popped up and called, and console.log is pushed into the call to start execution. When the request is completed, the completion event is triggered and a message is added to the message queue. The message queue first checks whether the call is idle. If the call is not idle, it will wait until the call is idle and pops up the head of the message queue. At this time, the callback function associated with the message is executed.
summary
The above is a conceptual summary of the non-blocking model and event loop. The mechanism of this event loop is not only unique to Node.js, and the code of Node.js is executed by a single thread. What are the advantages when facing a large number of concurrent requests?
The above picture shows the architecture diagram of Node.js. There is a module in the underlying layer of Node.js is responsible for maintaining the thread pool. When an IO request is issued, the underlying layer of Node.js will create a new thread to process the request, and then return the result to the upper layer after completion. Then, when there are multiple requests, the underlying module of Node.js will use as few threads as possible to complete the most tasks. If there are free threads, it will continue to be used to do other things. This is undoubtedly much smarter and more efficient for the opening of a new process or thread for each request as mentioned earlier.
This article is a summary of learning Node.js. If there are any problems or shortcomings, criticism and correction are welcome.