【问题标题】:How to call a Meteor method from the command line如何从命令行调用 Meteor 方法
【发布时间】:2018-07-18 17:53:54
【问题描述】:

我有一个 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

【问题讨论】:

    标签: meteor methods command-line


    【解决方案1】:

    您可以使用node ddp package 在您使用特定脚本创建的自己的 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 命令,而是使用您安装的nodenpm

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

    【讨论】:

    • 非常感谢您详细而清晰的回答。我已经尝试过了,这似乎正是我所需要的。
    • 请查看@FlissHou 的答案,如果可行,您可能宁愿接受它,因为流星外壳是为您的目的而直接构建的。
    • 我只是注意到您的答案似乎使用了流星壳。它适用于生产版本吗?
    • 是的,上面的代码应该可以在生产环境中运行,因为它可以在服务器上没有安装流星框架而只在节点运行时运行。编辑:我将编辑我的帖子并发表评论。
    • 谢谢,你能举个例子来说明该命令如何用于生产吗? DDP_PATH=$(npm root -g)/ddp node ddptest.js 给出错误“找不到模块'/home/user/Meteor/ddptest.js'”
    【解决方案2】:

    您可以查看此文档: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?”的回答

    【讨论】:

    • 在运行生产版本(即未安装 Meteor)时是否可以使用?
    • @LittleBrain 我并没有真正在生产版本中尝试过,但我认为如果没有安装流星,它就无法工作。
    猜你喜欢
    • 1970-01-01
    • 2021-06-02
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多