我能夠fork一個子進程,但我有問題看到該文件中發生了什麼錯誤,並且我知道該文件有錯誤,因爲我創建了一個沒有關閉}
所以應該發生錯誤。使用下面的代碼,我得到什麼(在控制檯和/或瀏覽器):節點fork子進程,並得到它的錯誤
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');
});
'child.stderr'? – zerkms
我也試過了,它不起作用 –
什麼「不起作用」是什麼意思?你試過什麼? – zerkms