首页 > 解决方案 > 如何从命令行调用 Meteor 方法

问题描述

我有一个 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');
}

我想过使用表单,但我想不出如何创建提交事件,因为 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

标签: meteormethodscommand-line

解决方案


您可以使用节点 ddp 包在您使用特定脚本创建的自己的 js 文件中调用 Meteor 方法。从那里您可以将所有输出管道传输到您想要的任何地方。

让我们假设以下 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) 并让您的 shell 处理路径解析。

为了获得安装路径,您可以使用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 命令,但使用你安装的nodeand npm

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


推荐阅读