node.js 没有使用 child_process 与子交互

Posted

技术标签:

【中文标题】node.js 没有使用 child_process 与子交互【英文标题】:node.js no interaction with child using child_process 【发布时间】:2013-12-07 03:37:29 【问题描述】:

全部,

尝试使用node.js隔离别人通过node.js中的child_process API创建的JS插件。我无法让子进程与父进程交互,运行此代码没有错误,但客户端无法发送或接收消息,或写入 console.log,所以我假设 JS 没有运行。我确实收到一条控制台消息,说明调试器正在从父级和客户端运行。代码几乎直接来自 node.js API 文档,所以我不确定为什么这不起作用。可能是 Windows 环境有问题?

父代码:

var fork = require('child_process').fork;

console.log('Starting plugin server...');

var myChild = fork('client.js');

myChild.on('message', function (msg) 
    console.log('PARENT got message:', msg)
);

myChild.send( hello: 'world' );

client.js:

console.log("child");

process.on('message', function (msg) 
    console.log('CHILD got message:', msg)
    process.send(msg: 'Message receivd & resent from the child.')
);

process.send(msg: 'Message from the child.');

输出:

debugger listening on port 5858
Starting plugin server...
debugger listening on port 5858

我正在使用 Windows 8.1、Visual Studio 2013 Pro 以及用于 node.js 插件的新工具。

任何指针表示赞赏。我是 node.js 的新手,所以可能会犯一个愚蠢的错误??

【问题讨论】:

好的,这里再进一步。似乎是 Visual Studio 环境或 VS 的 node.js 工具。在没有调试器(ctrl-F5)的情况下运行应用程序时,我得到了正确的行为。可能与使用相同调试 TCP 端口的孩子有关,或者 VS 调试器在运行 2 个节点实例时感到困惑。这仍然是一个问题,因为我需要能够调试插件,但会尝试 VS node.JS 工具中的远程调试器选项。我猜父母和孩子使用相同的调试端口将是一个问题,并且 VS node.js 工具无法更改端口。 【参考方案1】:

我在 windows 机器上遇到了 fork() 的问题。解决方案/解决方法是静默模式和日志记录。这是链接https://github.com/rogerwang/node-webkit/issues/1516

【讨论】:

感谢 kraag22,虽然不是同一个问题(这与使用 Visual Studio node.js 工具 alpha 进行调试有关),但您的链接是一个有用的参考,谢谢。

以上是关于node.js 没有使用 child_process 与子交互的主要内容,如果未能解决你的问题,请参考以下文章

使用 Sails.JS 框架的 Node.JS -- Passport:没有在名称下注册策略:facebook

你如何在 Node.js 中模拟 MySQL(没有 ORM)?

在没有回调的情况下使用 Node.js 流

为啥 Node.js 没有原生 DOM?

在 Node.js 程序上使用 DTrace 时没有函数名称

Spotify API Web:没有 Node.js 的 OAuth