如何从MeteorJS应用程序运行在meteor服务器上的远程外部命令?

我创build了一些login到Duolingo的CasperJS脚本,点击模块并打开,如果我在那里玩。

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

我已经阅读了关于MeteorJS远程过程调用的RPC,并且我已经阅读了PHP和NodeJS,你可以运行一个执行脚本的函数,就像我input命令来运行脚本一样。 我find了这些资源:ShellJS: https : //github.com/shelljs/shelljs和NodeJSsubprocess: https ://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback。

但我没有太多的经验,我正在这样做,以更多地了解CasperJS,MeteorJS。

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

非常感谢您的帮助。

这是一个“简单”,如果你知道该怎么做:-)

只是想知道会发生什么:

1.)在服务器端创build一个可以运行外部进程的方法
2.)你创build一个可以被客户端调用的meteor远程方法
3.)你在客户端创build动作并调用远程meteor方法
4.)绑定click事件来调用客户端上的操作

调用外部进程的方法

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)和客户端部分的文件中,用你想要按下的button(将mytemplate重命名为你定义的)。

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

干杯汤姆