节点分叉子进程并获取它's错误
Node fork child process and get it's errors
我可以派生一个子进程,但我在查看该文件中发生的错误时遇到问题,我知道该文件有错误,因为我创建了一个没有关闭}
的对象,所以应该会发生错误。使用以下代码,我什么也得不到(在控制台和/或浏览器中):
var child = require('child_process').fork(full_path, [], {
silent: true,
});
// Tried both of these and nothing gets displayed
child.stdout.on('error', function(data){
res.write(data);
console.log(data);
});
child.stderr.on('error', function(data){
res.write(data);
console.log(data);
});
以下是子进程:
var sys = require('sys');
var mustache = require('mustache');
var template = require('./templates/mypage.html');
sys.puts(template);
var view = {
"musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"]
; // Forced error here with no closing "}"
var html = mustache.to_html(template, view);
sys.puts(html);
我需要做些什么来显示错误?我做错了什么?
编辑
完整脚本:
var sys = require('sys');
var config = require('./server.json');
var url = require('url');
var http = require('http');
function handleRequest(req, res){
var full_path = "";
for(var i in config){
var domain = config[i].server;
if(req.headers.host === domain){
var info = url.parse(req.url);
full_path = config[i].root + info.pathname;
}
}
console.log("Page: " + full_path);
if(full_path != ""){
var child = require('child_process').fork(full_path, [], {
silent: true,
});
child.stdout.on('data', function(data){
res.write(data);
});
child.stdout.on('error', function(data){
res.write(data);
console.log(data);
});
child.stderr.on('error', function(data){
res.write(data);
console.log(data);
});
child.stdout.on('end', function(){
res.end();
});
}else{
res.end();
}
}
var server = http.createServer(handleRequest);
server.listen(3000, function(err){
console.log(err || 'Server listening on 3000');
});
默认情况下,所有输入/输出都返回到父级。
给定parent.js
var fork = require('child_process').fork;
var child = fork('./child');
child.js
的输出和错误返回到父
setInterval(function() {
console.log('I am here')
}, 1000);
setTimeout(function() {
throw new Error('oops')
}, 5000);
就像@柏拉图说的,{ silent: true }
会关闭
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 铬:“;未捕获的语法错误:意外的标记:"
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 相位器状态未捕获参考错误
- /undefined在我的404错误日志中多次出现
- Javascript未捕获语法错误意外的标识符错误
- javascript:如何在antlr生成的Lexer中进行错误处理
- Angularjs工厂注入错误
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- 为什么忽略了eval()代码中的语法错误