Let’s take a look at what blocking operations are;
I simulate a sleep() method to delay printing of hello star for 10 seconds.
requestHandlers.js
When requesting /start, there is a delay of 10 seconds before printing.
When requesting/upload, it will not be affected.
Let’s do an experiment:
Enter http://localhost:8888/start in the address bar of the first browser window, but don’t open it yet!
Enter http://localhost:8888/upload in the address bar of the second browser window. Again, don’t open it yet!
We press Enter in the first window ("/start"), and then quickly switch to the second window ("/upload") and press Enter.
Attention, what happened:
The/start URL took 10 seconds to load, which is what we expected.
The/upload URL also took 10 seconds!
Yes, it does not have operations similar to sleep() in the corresponding request handler. What is the problem?
The reason is that start() contains blocking operations. To put it figuratively, "it blocks all other processing work."
Node.js is single-threaded and can process tasks in parallel without adding additional threads.
It implements parallel operations through event loop. We should make full use of this - avoid blocking operations as much as possible and use non-blocking operations instead.
In the next section we will introduce how to implement non-blocking operations.