Meteor JS simulate server command line on client html

后端 未结 2 1194
情书的邮戳
情书的邮戳 2021-02-03 15:04

I am new to Meteor and want to make a simple app. I am failing to simulate the command line on the server side according to http://terokaisti.blogspot.com/2012/10/writing-termin

相关标签:
2条回答
  • 2021-02-03 15:56

    Instead of using the fibers npm module directly, we now have Meteor.bindEnvironment as stated here:

    Future.wait() can't wait without a fiber (while waiting on another future in Meteor.method)

    and here:

    http://docs.meteor.com/#/full/renderable_content

    0 讨论(0)
  • 2021-02-03 15:57

    Here is a working example. The output will be in terminal. Hope that helps.

    terminal.html

    <head>
      <title>terminal</title>
    </head>
    
    <body>
      {{> hello}}
    </body>
    
    <template name="hello">
      <input type="text" id="command">
      <input type="button" id="button" value="Click" />
    </template>
    

    terminal.js

    Replies = new Meteor.Collection('replies');
    
    
    if (Meteor.isClient) {
      Template.hello.greeting = function () {
        return "Welcome to terminal.";
      };
    
      Template.hello.events({
        'click #button': function () {
          console.log("clicking");
          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();
          }); 
        }
      });
    }
    
    0 讨论(0)
提交回复
热议问题