加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – Meteor JS在客户端html上模拟服务器命令行

发布时间:2020-12-15 16:59:34 所属栏目:安全 来源:网络整理
导读:我是Meteor的新手,想要制作一个简单的应用程序.根据 http://terokaisti.blogspot.com/2012/10/writing-terminal-app-with-meteor-js.html,我无法模拟服务器端的命令行 在客户端(Mac OSX Mavericks),结果只是空白我输入命令并点击“运行”按钮.我正在使用上面
我是Meteor的新手,想要制作一个简单的应用程序.根据 http://terokaisti.blogspot.com/2012/10/writing-terminal-app-with-meteor-js.html,我无法模拟服务器端的命令行

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

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

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

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

我错过了什么?我怎么调试?提前致谢!

这是一个有效的例子.输出将在终端中.希望有所帮助.

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,stderr) {
        console.log('Command Method',error,stderr);
        Fiber(function() {
          Replies.remove({});
          var replyId = Replies.insert({message: stdout ? stdout : stderr});
          return replyId;  
        }).run();
      }); 
    }
  });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读