Node.js - core cluster module
-
29-10-2019 - |
Question
I am using the core cluster module in node v0.6.5. I have the following code:
var cluster = require('cluster');
var http = require('http');
var numWorkers = 3;
var count = 0;
if (cluster.isMaster) {
for (var i = 0; i < numWorkers; i++) {
cluster.fork();
}
} else {
console.log('createServer called');
http.createServer(function (req, res) {
count++;
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Count is: ' + count.toString());
}).listen(1337, "127.0.0.1");
}
Everytime I hit the page, the count is incremented twice, e.g. 1,3,5,7... Why does it increment twice?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow