I have a problem with Visual Studio code with Cluster
Edit
If I pressed Ctrl + F5 , it works correctly, what does it do other than just F5 , do I always need to run the command with Ctrl?
---
It seems that workers never start at startup using the VS Code Launch (F5) command . Do I need to make some changes to the .vscode / launch.json file in order to make the cluster work by working out.
The actual code is copied from Node.js 6 api https://nodejs.org/api/cluster.html#cluster_cluster
npm test . The Windows command line shows the following:
Master started
Listening port 80
Listening port 80
Listening port 80
Listening port 80
VS (F5) :
node --debug-brk=7601 --nolazy index.js
Debugger listening on port 7601
Master started
Debugger listening on port 7602
Debugger listening on port 7603
Debugger listening on port 7604
Debugger listening on port 7605
VS Code launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/index.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
..........
index.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log('Master started')
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(80);
console.log('Listening port 80')
}