javascript - Node.js v6.0.0 上的 Visual Studio 代码 (F5) 集群问题

标签 javascript node.js npm visual-studio-code node-cluster

我在 Visual studio codeCluster 方面遇到一些问题

编辑

如果我按下 Ctrl + F5 它会正常工作,除了 F5 之外它还做了什么,我是否需要始终使用 Ctrl 启动命令?

---

使用 VS Code Launch 命令 (F5) 启动时,worker 似乎永远不会启动。我是否需要对 .vscode/launch.json 文件进行一些更改以使 Cluster 正常工作。

实际代码是从 Node.js 6 api 复制的 https://nodejs.org/api/cluster.html#cluster_cluster

npm test Windows 命令提示符显示:

Master started
Listening port 80
Listening port 80
Listening port 80
Listening port 80

VS Code (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) {
    // Fork workers.
    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 {
    // Workers can share any TCP connection
    // In this case it is an HTTP server
    http.createServer((req, res) => {
        res.writeHead(200);
        res.end('hello world\n');
    }).listen(80);
    console.log('Listening port 80')
}

最佳答案

我遇到了同样的问题。 weinand 在 https://github.com/Microsoft/vscode/issues/3201 中描述的第二个解决方法对我有用:

Launch node from a terminal and attach to it with the VS Code debugger.

Run in terminal: node --debug app.js

Then select the default 'attach' launch config and attach to it.

The workaround is the preferred way if you actually want to debug any worker and not just the first process that is launched.

关于javascript - Node.js v6.0.0 上的 Visual Studio 代码 (F5) 集群问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37012179/

相关文章:

node.js - 无法删除 Node Express 中的 x-powered-by header

css - Electron 不使用更新的 css 文件

python - 为使用 nodejs 调用的脚本导入 python 包

javascript - 更新到 Chrome v65 并开始收到此错误 : Expected onClick listener to be a function, 而不是收到类型字符串

javascript - Youku JS API - 播放/暂停方法和事件

node.js - 将 Joi 升级到最新版本后出错 - 架构只能包含普通对象(名称)

node.js - Ubuntu上的node_modules(或npm)有什么问题

javascript - React 模块安装失败

javascript - fetch 给出一个空的响应主体

javascript - 外部JS未定义