如何从命令行调用 Meteor 方法
How to call a Meteor method from the 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');
}
我想过使用表单,但我想不出如何创建提交事件,因为 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 错误,我不知道如何 'downgrade' 回到支持的 59,禁用 Firefox 自动更新的选项似乎不再存在。错误是:
c is undefined
您可以使用 node ddp package 在您使用特定脚本创建的自己的 js 文件中调用 Meteor 方法。从那里你可以把所有的输出都传送到你想要的任何地方。
让我们假设以下 Meteor 方法:
Meteor.methods({
'myMethod'() {
console.log("hello console")
return "hello result"
}
})
接下来的步骤将允许您从另一个 shell 调用此方法,假设您的 Meteor 应用程序是 运行。
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
到作为 运行 你的流星应用程序的开放会话。
编辑:关于在生产中使用它的注释
如果你想在生产中使用这个脚本,你必须使用 shell 命令而不使用 meteor
命令,但使用你安装的 node
和 npm
。
如果您遇到路径问题,请使用 process.execPath
查找您的节点二进制文件并使用 npm root -g
查找您的全局 npm 模块。
您可以查看此文档:Command Line | meteor shell。
当您的 meteor 应用程序为 运行 时,您可以执行 meteor shell
以启动交互式控制台。在控制台中,您可以执行 Meteor.call(...)
.
因此,如果您想使用 meteor shell
编写脚本,您可能需要为 meteor shell
传输脚本文件。喜欢,
$ meteor shell < script_file
另见“”的回答
我有一个 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 错误,我不知道如何 'downgrade' 回到支持的 59,禁用 Firefox 自动更新的选项似乎不再存在。错误是:
c is undefined
您可以使用 node ddp package 在您使用特定脚本创建的自己的 js 文件中调用 Meteor 方法。从那里你可以把所有的输出都传送到你想要的任何地方。
让我们假设以下 Meteor 方法:
Meteor.methods({
'myMethod'() {
console.log("hello console")
return "hello result"
}
})
接下来的步骤将允许您从另一个 shell 调用此方法,假设您的 Meteor 应用程序是 运行。
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
到作为 运行 你的流星应用程序的开放会话。
编辑:关于在生产中使用它的注释
如果你想在生产中使用这个脚本,你必须使用 shell 命令而不使用 meteor
命令,但使用你安装的 node
和 npm
。
如果您遇到路径问题,请使用 process.execPath
查找您的节点二进制文件并使用 npm root -g
查找您的全局 npm 模块。
您可以查看此文档:Command Line | meteor shell。
当您的 meteor 应用程序为 运行 时,您可以执行 meteor shell
以启动交互式控制台。在控制台中,您可以执行 Meteor.call(...)
.
因此,如果您想使用 meteor shell
编写脚本,您可能需要为 meteor shell
传输脚本文件。喜欢,
$ meteor shell < script_file
另见“