如何在Meteor中将exec命令的输出变量返回给客户端?

时间:2015-07-13 13:45:52

标签: javascript meteor exec

让我们从this question获取此示例应用程序,它允许您运行终端并在输出执行到集合后插入输出。我想知道解决方案可能是什么,在客户端返回带有结果的变量而不使用集合?



<head>
  <title>terminal</title>
</head>

<body>
  {{> hello}}
</body>

<template name="hello">
  <input type="text" id="command">
  <input type="button" id="button" value="Click" />
</template>
&#13;
&#13;
&#13;

&#13;
&#13;
Replies = new Meteor.Collection('replies');


if (Meteor.isClient) {
  Template.hello.events({
    'click #button': function () {
      var cmd = $("input#command").val();
      console.log("command", cmd);
      var replyId = Meteor.call('command', cmd);
      Session.set('replyId', replyId);
    }
  });
}

if (Meteor.isServer) {
  exec = Npm.require('child_process').exec;
  Meteor.methods({
    'command' : function(line) {
      console.log("In command method", line);
      Fiber = Npm.require('fibers');
      exec(line, function(error, stdout, stderr) {
        console.log('Command Method', error, stdout, stderr);
        Fiber(function() {
          Replies.remove({});
          var replyId = Replies.insert({message: stdout ? stdout : stderr});
          return replyId;  
        }).run();
      }); 
    }
  });
}
&#13;
&#13;
&#13;

0 个答案:

没有答案
相关问题