我正在使用一个小的Meteor应用程序,我需要对另一个程序执行一些命令,获取这些结果,然后在服务器端使用这些结果。我找到了一些类似以下的类似答案,但它们并不是我所需要的:
例子
客户端显示有关存储在第三方服务上的文件的信息
当客户端请求文件时,服务器调用另一个程序(在本例中为bash脚本)来检索文件
检索后,该文件可供客户端下载
我已经在#1和#3上工作了,减去了一些额外的测试,一旦我弄清楚了如何做#2,我将对其进行整合。我想我可能需要的是Meteor执行驻留在服务器上的任意代码的方法?
这篇博客文章展示了如何使用fibers
NPM的软件包在Meteor中运行shell命令。
要安装光纤:
npm -g install fibers
要运行您的bash代码,请执行以下操作:
// Load future from fibers
var Future = Npm.require("fibers/future");
// Load exec
var exec = Npm.require("child_process").exec;
// Server methods
Meteor.methods({
runCode: 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();
var future=new Future();
var command="pwd";
exec(command,function(error,stdout,stderr) {
if(error) {
console.log(error);
throw new Meteor.Error(500,command+" failed");
}
future.return(stdout.toString());
});
return future.wait();
}
});
您也可以为此尝试一些AtmosphereJS软件包,例如jchristman:exec。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句