node.js - 如何从 MeteorJS 应用程序在 Meteor 服务器上运行远程外部命令?

标签 node.js meteor casperjs

我创建了一些 CasperJS 脚本来登录 Duolingo,单击一个模块并打开,就像我在那里玩一样。

我创建了一个简单的meteorJS应用程序,我希望当我单击按钮时能够执行该casperjs脚本。我正在寻找有这种经验的人来帮助我或以正确的方式引导我,因为我不太知道我可以用什么来实现这个小小的个人游戏。

我读过有关 RPC - MeteorJS 的远程过程调用的内容,并且我读过,使用 PHP 和 NodeJS,您可以运行一个执行脚本的函数,就像我键入命令来运行脚本一样。 我找到了这些资源: ShellJS:https://github.com/shelljs/shelljs 和 NodeJS 子进程:https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback .

但是我没有太多经验,我这样做是为了了解更多关于CasperJS、MeteorJS的知识。

我需要的是能够使用我的 Meteorjs 应用程序运行此命令 ->“casperjs duolingo.js --engine=slimerjs --disk-cache=no”,这样我就可以继续创建我的小自动化机器人来玩 Duolingo 整体。

非常感谢您的帮助。

最佳答案

如果您知道该怎么做,这就是“简单”:-)

只是想知道会发生什么:

1.) 您在服务器端创建一个可以运行外部进程的方法
2.) 创建一个可以被客户端调用的meteor远程方法
3.) 您在客户端创建操作并调用远程 meteor 方法
4.) 您绑定(bind)点击事件以调用客户端上的操作

调用外部进程的方法

process_exec_sync = function (command) {
  // Load future from fibers
  var Future = Npm.require("fibers/future");
  // Load exec
  var child = Npm.require("child_process");
  // Create new future
  var future = new Future();
  // Run command synchronous
  child.exec(command, function(error, stdout, stderr) {
    // return an onbject to identify error and success
    var result = {};
    // test for error
    if (error) {
      result.error = error;
    }
    // return stdout
    result.stdout = stdout;
    future.return(result);
  });
  // wait for future
  return future.wait();
}

Meteor远程服务器方法

// define server methods so that the clients will have access to server components
Meteor.methods({
  runCasperJS: function() {
    // This method call won't return immediately, it will wait for the
    // asynchronous code to finish, so we call unblock to allow this client
    // to queue other method calls (see Meteor docs)
    this.unblock();
    // run synchonous system command
    var result = process_exec_sync('casperjs duolingo.js --engine=slimerjs --disk-cache=no');
    // check for error
    if (result.error) {
      throw new Meteor.Error("exec-fail", "Error running CasperJS: " + result.error.message);
    }
    // success
    return true;
  }
})

客户端事件和远程方法调用

Template.mytemplate.events({
  'click #run-casper': function(e) {
    // try to run remote system call
    Meteor.call('runCasperJS', function(err, res) {
      // check result
      if (err) {
        // Do some error notification
      } else {
        // Do some success action
      }
    });
  }
});

继续

您需要将服务器端方法放入目录“yourproject/server”(例如)main.js 中的文件中,并将客户端部分放入带有您希望按下的按钮的模板中(将 mytemplate 重命名为您定义的按钮)。

希望你能得到你需要的东西。

干杯 汤姆

关于node.js - 如何从 MeteorJS 应用程序在 Meteor 服务器上运行远程外部命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38925987/

相关文章:

javascript - 单击按钮直到它在 CasperJS 中消失

javascript - "catch(console.error)"是捕获 Promise 拒绝的有效方法吗?

node.js - 如何从mongodb中的多个值中查找

node.js - 每次测试前重置数据库

css - Bootstrap 包在 Meteor 中不起作用

javascript - 将参数传递给 casperjs.start

javascript - 为什么只有在集合不存在时 MongoDB 才插入记录的速度较慢?

meteor 黑客 :aggregate error

javascript - Meteor 和 mongodg 在 debian wheezy 上退出代码 1

node.js - 为什么 Phantom、Casper 和 Meteor 需要它们自己的可执行文件?