Mein purpose is simply to launch an exe using node.js. In case the application is unexpectedly terminated, the exception should be catched and logged in a protocol. An exception is thrown thru the following statement in a c++ project:
throw std::string("NOT X or x!!!");
My Javascript is:
var spawn = require('child_process').spawn;
var cp = spawn(process.env.comspec, ['/c', 'myExcepTest.exe', '', '']);
// **doesn't work**
cp.on('uncaughtException', function(err){
console.log("Caught exception: " + err);
});
cp.stderr.on('error', function(err) {
console.log(err.toString());
});
cp.stdin.resume();
cp.stdin.setEncoding('utf8');
cp.stdin.on('data', function(data) {
cp.stdout.write(data);
});
// **does work**
cp.on('exit', function(code){
console.log("Child exited with code: " + code);
});
cp.stdout.on('data', function(data) {
console.log(data.toString());
});
My questions are:
Is it a good practice to use node.js to monitor a process on windows?
Is it possible, to use node.js to catch an exception on windows?
How to correctly pipe the stdin to stdout in a node.js script?
Sorry for my poor English ;)
uncaughtException
for child processes. The reason being is that there is no standard way for catching such things, considering the child process could be any kind of program.stdin
of a child process to its own stdout
, because stdin
is a Writable stream and is an input to the child process (not an output from it), so stdin
will never have data
events.