在 Meteor/Node 中确定从服务器返回的输出是客户端上的 stderr 还是 stdout?
Determine if returned output from server is stderr or stdout on client in Meteor/Node?
我运行 执行用户给出的命令,return 它输出到客户端。当我 return 只是结果时一切正常,但我需要 运行 两种不同的 if 场景,基于 stdout 和 stderr。如何确定 returned 输出是 stdout 还是 stderr?在这种情况下,它总是 运行s 像标准输出。*
*我需要直接解决方案,希望避免使用集合。请注意,这只是示例代码。
服务器:
// load future from fibers
var Future = Meteor.npmRequire("fibers/future");
// load exec
var exec = Meteor.npmRequire("child_process").exec;
Meteor.methods({
'command' : function(line) {
// this method call won't return immediately, it will wait for the
// asynchronous code to finish, call unblock to allow this client
this.unblock();
var future = new Future();
exec(line, function(error, stdout, stderr) {
if(stdout){
console.log(stdout);
future.return(stdout);
} else {
console.log(stderr);
future.return(stderr);
}
});
return future.wait();
}
});
客户:
var line = inputdl.value;
Meteor.call('command', line, function(error, stdout, stderr) {
if(stdout){
console.log(stdout);
} else {
alert('Not valid command: ' + stderr);
}
});
您可以 return 包含 stdout 和 stderr 的对象:
Meteor.methods({
'command' : function(line) {
this.unblock();
var future = new Future();
exec(line, function(error, stdout, stderr) {
future.return({stdout: stdout, stderr: stderr});
});
return future.wait();
}
});
在客户端上:
Meteor.call('command', line, function(error, result) {
if(result.stdout){
console.log(result.stdout);
} else {
alert('Not valid command: ' + result.stderr);
}
});
我运行 执行用户给出的命令,return 它输出到客户端。当我 return 只是结果时一切正常,但我需要 运行 两种不同的 if 场景,基于 stdout 和 stderr。如何确定 returned 输出是 stdout 还是 stderr?在这种情况下,它总是 运行s 像标准输出。*
*我需要直接解决方案,希望避免使用集合。请注意,这只是示例代码。
服务器:
// load future from fibers
var Future = Meteor.npmRequire("fibers/future");
// load exec
var exec = Meteor.npmRequire("child_process").exec;
Meteor.methods({
'command' : function(line) {
// this method call won't return immediately, it will wait for the
// asynchronous code to finish, call unblock to allow this client
this.unblock();
var future = new Future();
exec(line, function(error, stdout, stderr) {
if(stdout){
console.log(stdout);
future.return(stdout);
} else {
console.log(stderr);
future.return(stderr);
}
});
return future.wait();
}
});
客户:
var line = inputdl.value;
Meteor.call('command', line, function(error, stdout, stderr) {
if(stdout){
console.log(stdout);
} else {
alert('Not valid command: ' + stderr);
}
});
您可以 return 包含 stdout 和 stderr 的对象:
Meteor.methods({
'command' : function(line) {
this.unblock();
var future = new Future();
exec(line, function(error, stdout, stderr) {
future.return({stdout: stdout, stderr: stderr});
});
return future.wait();
}
});
在客户端上:
Meteor.call('command', line, function(error, result) {
if(result.stdout){
console.log(result.stdout);
} else {
alert('Not valid command: ' + result.stderr);
}
});