简体   繁体   中英

Determine if returned output from server is stderr or stdout on client in Meteor/Node?

I run exec command given by user and return it's output to client. Everything works fine when I return just result, but I need to run two different if scenarios, based on stdout and stderr. How to determine if returned output is stdout or stderr? In this scenario it always runs like stdout.*

*I need direct solution, want to avoid using collections. Note this is just example code.

SERVER:

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

CLIENT:

 var line = inputdl.value; Meteor.call('command', line, function(error, stdout, stderr) { if(stdout){ console.log(stdout); } else { alert('Not valid command: ' + stderr); } }); 

You can return an object containing both stdout and 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();
  }
});

and on the client:

Meteor.call('command', line, function(error, result) {
  if(result.stdout){
    console.log(result.stdout);
  } else {
    alert('Not valid command: ' + result.stderr); 
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM