【发布时间】:2015-03-03 23:37:34
【问题描述】:
为了发送 JSON,我启用了父子进程之间的通信,如下所示:
孩子:
try {
var price1 = parseInt(process.argv[2]);
if (!price1) {
throw new Error('Price in calculations.js undefined');
}
var result = {
'timeStamp' : Date(),
'prices' : { 'player1' : price1, 'player2' : 666}
};
process.send(result);
} catch (e) {
// In case of an error, I get here as expected.
process.send(e);
}
家长:
var spawn = require('child_process').spawn;
var child = spawn('node', ['calculations.js', 333], {stdio: [null,null,'pipe','ipc']});
child.on('message', function(data) {
if (data instanceof Error) {
// In case of an error, this is never reached.
} else {
// do sthing with JSON object.
}
});
JSON 的东西工作正常。但如果我引发错误,它就不起作用。我想将整个错误对象(带有消息和堆栈跟踪)从子级发送到父级。但这似乎不是我发送的错误实例。
【问题讨论】:
-
你
provoke哪个错误? -
我更新了帖子。我只是设置了 process.argv[3] 然后抛出我的自定义错误。
-
你正在用 3 个参数生成进程,为什么会抛出错误
-
这与在 shell 中执行 'node computed.js 333' 相同。 process.argv[0] 将是“节点”,process.argv[1] 是计算.js 的路径,而 process.argv[2] 是 333。process.argv[4] 是未定义的,这就是我抛出错误的原因.
标签: javascript node.js inter-process-communicat