Meteor JS simulate server command line on client html

后端 未结 2 1193
情书的邮戳
情书的邮戳 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:57

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

    terminal.html

    
      terminal
    
    
    
      {{> hello}}
    
    
    
    

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

提交回复
热议问题