如何从命令行调用Meteor方法

时间:2018-07-16 11:20:18

标签: meteor methods command-line

我有一个Meteor应用程序,想从命令行调用服务器方法,以便可以编写bash脚本来执行预定的操作。

是否可以直接调用方法或提交表单然后触发服务器端代码?

我尝试使用curl来调用方法,但是这是不可能的,或者我缺少一些基本的知识。这不起作用:

curl "http://localhost:3000/Meteor.call('myMethod')"

也没有:

curl -s -d "http://localhost:3000/imports/api/test.js" > out.html

其中test.js:

var test = function(){
    console.log('hello');
}

我考虑过使用表单,但是我想不起来如何创建Submit事件,因为Meteor客户端使用模板事件来调用服务器方法。

我将非常感谢您的帮助!感觉这应该是一件简单的事情,但让我感到难过。

编辑:我也尝试过通过casperjs运行phantomjs和slimerjs。

phantomjs不再维护,并生成错误:

TypeError: Attempting to change the setter of an unconfigurable property

https://github.com/casperjs/casperjs/issues/1935

Firefox 60出现

slimerjs错误,我不知道如何将其“降级”回支持的59,并且似乎不再存在禁用Firefox自动更新的选项。错误是:

c is undefined

https://github.com/laurentj/slimerjs/issues/694

2 个答案:

答案 0 :(得分:2)

您可以利用node ddp package在使用特定脚本创建的自己的js文件中调用Meteor方法。从那里,您可以将所有管道通过管道传输到所需的任何地方。

我们假设以下流星方法:

Meteor.methods({
  'myMethod'() {
    console.log("hello console")
    return "hello result"
  }
})

假设您的Meteor应用程序正在运行,接下来的步骤将使您可以从另一个shell调用此方法。

1。在全局npm目录中安装ddp

$ meteor npm install -g ddp

2。创建脚本以在测试目录中调用您的方法

$ mkdir -p  ddptest
$ cd ddptest
$ touch ddptest.js

使用您选择的编辑器或命令将ddp脚本代码放入文件中。  (以下代码可从软件包的自述文件中免费获取。可以根据需要进行配置。)

ddptest / ddptest.js

var DDPClient = require(process.env.DDP_PATH);

var ddpclient = new DDPClient({
  // All properties optional, defaults shown
  host : "localhost",
  port : 3000,
  ssl  : false,
  autoReconnect : true,
  autoReconnectTimer : 500,
  maintainCollections : true,
  ddpVersion : '1',  // ['1', 'pre2', 'pre1'] available
  // uses the SockJs protocol to create the connection
  // this still uses websockets, but allows to get the benefits
  // from projects like meteorhacks:cluster
  // (for load balancing and service discovery)
  // do not use `path` option when you are using useSockJs
  useSockJs: true,
  // Use a full url instead of a set of `host`, `port` and `ssl`
  // do not set `useSockJs` option if `url` is used
  url: 'wss://example.com/websocket'
}); 

ddpclient.connect(function(error, wasReconnect) {
  // If autoReconnect is true, this callback will be invoked each time
  // a server connection is re-established
  if (error) {
    console.log('DDP connection error!');
    console.error(error)
    return;
  }

  if (wasReconnect) {
    console.log('Reestablishment of a connection.');
  }

  console.log('connected!');

  setTimeout(function () {
    /*
     * Call a Meteor Method
     */
    ddpclient.call(
      'myMethod',             // namyMethodme of Meteor Method being called
      ['foo', 'bar'],            // parameters to send to Meteor Method
      function (err, result) {   // callback which returns the method call results
        console.log('called function, result: ' + result);
        ddpclient.close();
      },
      function () {              // callback which fires when server has finished
        console.log('updated');  // sending any updated documents as a result of
        console.log(ddpclient.collections.posts);  // calling this method
      }
    );
  }, 3000);
});

代码假定您的应用程序在localhost:3000上运行,请注意,在错误或不良行为方面没有任何关闭。

如您在顶部看到的,该文件将导入您全局安装的ddp软件包。现在,为了在不使用其他工具的情况下获取路径,只需传递一个环境变量(process.env.DDP_PATH),然后让您的外壳程序处理路径解析。

要获取安装路径,可以将npm root与全局标志一起使用。

最后通过以下方式调用脚本:

$ DDP_PATH=$(meteor npm root -g)/ddp meteor node ddptest.js

将为您提供以下输出:

connected!
updated
undefined
called function, result: hello result

并将hello console记录到运行流星应用程序的打开会话中。

编辑:在生产中使用此注释

如果要在生产环境中使用此脚本,则必须使用不带meteor命令的shell命令,而要使用nodenpm的安装。

如果您在使用路径时遇到麻烦,请使用process.execPath查找节点二进制文件,并使用npm root -g查找全局npm模块。

答案 1 :(得分:1)

您可以查看以下文档:Command Line | meteor shell

在运行流星应用程序时,您可以执行meteor shell以启动交互式控制台。在控制台中,您可以执行Meteor.call(...)

因此,如果要使用meteor shell编写脚本,则可能需要通过管道传输meteor shell的脚本文件。喜欢,

$ meteor shell < script_file

另请参阅“ How can I pipe a command into the meteor shell?”的答案