Run this server module with node:
node httpServer.js
When our server starts, the console will display the following:
Output
Server is running on http://localhost:8000
Now, to test the performance of our module, open two additional terminals. In the first terminal, use the curl command to make a request to the /total endpoint, which we expect to be slow:
curl http://localhost:8000/total
In the other terminal, use curl to make a request to the /hello endpoint like this:
curl http://localhost:8000/hello
The first request will return the following JSON:
Output
{"totalCount":5000000000}
Whereas the second request will return this JSON:
Output
{"message":"hello"}
The request to /hello completed only after the request to /total. The slowFunction() blocked all other code from executing while it was still in its loop. You can verify this by looking at the Node.js server output that was logged in your original terminal:
Output
Returning /total results
Returning /hello results
To test the improvement using new Worker made on HTTP server, begin by executing the httpServer.js file with node:
node httpServer.js
Like before, it will output the following message when it launches:
Output
Server is running on http://localhost:8000
To test the server, we will need an additional two terminals as we did the first time. You can re-use them if they are still open.
In the first terminal, use the curl command to make a request to the /total endpoint, which takes a while to compute:
curl http://localhost:8000/total
In the other terminal, use curl to make a request to the /hello endpoint, which responds in a short time:
curl http://localhost:8000/hello
The first request will return the following JSON:
Output
{"totalCount":5000000000}
Whereas the second request will return this JSON:
Output
{"message":"hello"}
Unlike the first time we tried this, the second request to /hello runs immediately. You can confirm by reviewing the logs, which will look like this:
Output
Returning /hello results
Returning /total results
These logs show that the request for the /hello endpoint ran after the worker process was created but before the worker process had finished its task.
Since we moved the blocking code in a worker process using new Worker, the server was still able to respond to other requests and execute other JavaScript code. Because of the new Worker function’s message passing ability, we can control when a worker process begins an activity.