lundi 6 avril 2015

Node fork child process and get it's errors

I am able to fork a child process, but I am having issues seeing what errors are taking place in that file, and I know the file has errors, because I created an object without a closing } so an error should occur. Using the following code, I get nothing (in the console and/or the browser):



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);
});


Here is the child process:



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);


What do I need to do to display the errors? What am I doing incorrectly?


Edit


Full script:



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');
});

Aucun commentaire:

Enregistrer un commentaire