Thursday 19 July 2018

Cluster module isn't working

I don't know if i just messed something up or if the module is actually broken.I'm using Node version 8.11.3. I'm following a tutorial and i literally copied over the same code. Yet the results are different.Here's the code:const cluster = require('cluster'); // Check that the current instance is Master if (cluster.isMaster) { // Cluster Manager / Master cluster.fork(); cluster.fork(); cluster.fork(); cluster.fork(); } else { // Child / Worker const express = require('express'); const app = express(); function doWork(duration) { const start = Date.now(); while(Date.now() - start < duration) { } } app.get("/", (req, res) => { doWork(5000); res.send("This was slow"); }); app.get("/fast", (req, res) => { res.send("This was fast"); }); const port = 3000; app.listen(port, () => { console.log((`Server started on port ${port}`)); }); }When i test in browser, i fire up the root route and then the "fast" route. The fast route should load instantly because of clustering, and after 5 seconds the root route should load. But it doesn't happen. Both routes load simultaneously after 5 seconds. I'm at a loss.

Submitted July 19, 2018 at 01:42PM by Pelopida92

No comments:

Post a Comment