Meteor JS 在客户端 html 上模拟服务器命令行 [英] Meteor JS simulate server command line on client html

查看:40
本文介绍了Meteor JS 在客户端 html 上模拟服务器命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Meteor 的新手,想制作一个简单的应用程序.我无法根据 http 在服务器端模拟命令行://terokaisti.blogspot.com/2012/10/writing-terminal-app-with-meteor-js.html

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-terminal-app-with-meteor-js.html

当我在客户端(Mac OSX Mavericks)输入命令并点击运行"按钮时,结果只是空白.我正在使用上面站点中的确切代码,除了我有 autorun 和 exec = Npm.require('child_process').exec;

The result is just blank when on the client side (Mac OSX Mavericks) I type in a command and hit the "Run" button. I am using the exact code from the site above, except that I have autorun and exec = Npm.require('child_process').exec;

下面是我的 html 和 js 文件...

Below are my html and js files...

TerminalApp.html

TerminalApp.html

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

<body>
  {{> terminal}}
</body>

<template name="terminal">
 <pre>{{ window }}</pre>
<input id="command" type="text" value="{{ last_cmd }}" />
 <input type="button" value="Run" />
</template>

TerminalApp.js

TerminalApp.js

// A collection for stdouts
var Replies = new Meteor.Collection('replies');

if(Meteor.is_client) {

 // Start listening changes in Replies
    Meteor.autorun(function() {
        Meteor.subscribe('replies');
    });

 // Set an observer to be triggered when Replies.insert() is invoked
 Replies.find().observe({
  'added': function(item) {
   // Set the terminal reply to Session
   Session.set('stdout', item.message);
  }
 });

 // Show the last command in input field
 Template.terminal.last_cmd = function() {
 return Session.get('last_cmd');
 };

 // Show the last shell reply in browser
 Template.terminal.window = function() {
  return Session.get('stdout');
 };

 // Add an event listener for Run-button
 Template.terminal.events = {
  'click [type="button"]': function() {
   var cmd = $('#command').val();
   Session.set('last_cmd', cmd);

   // Call the command method in server side
   Meteor.call('command', cmd);
  }
 }; 
}

if(Meteor.is_server) {
 var exec;

 // Initialize the exec function
 Meteor.startup(function() {
  exec = Npm.require('child_process').exec;
 });

 // Trigger the observer in Replies collection
    Meteor.publish('replies', function() {
        return Replies.find();
    });

 Meteor.methods({
  'command': function(line) {
   // Run the requested command in shell
   exec(line, function(error, stdout, stderr) {
    // Collection commands must be executed within a Fiber
    Fiber(function() {
     Replies.remove({});
     Replies.insert({message: stdout ? stdout : stderr});
    }).run();
   });
  }
 });
}

我错过了什么?我该如何调试?提前致谢!

What am I missing? How can I debug? Thanks in advance!

推荐答案

这是一个工作示例.输出将在终端中.希望有所帮助.

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

终端.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>

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

这篇关于Meteor JS 在客户端 html 上模拟服务器命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆