我想用 Meteor 实现一个 webhook
我正在使用kadira/flowrouter Meteor插件但无法获取POST数据。 queryParams 或 params 都没有返回我想要获取的消息正文。
FlowRouter.route('/my_weebhook', {
action: function(params, queryParams) {
console.log(queryParams);
console.log(params);
}
});
最佳答案
我不知道如何使用kadira/flowrouter,但你可以使用Meteor基础包WebApp
达到你所需要的。下面是示例代码。您需要在 server.js 中编码或导入类似的内容
import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import url from "url";
WebApp.connectHandlers.use('/my_webhook', (req, res) => {
//Below two lines show how you can parse the query string from url if you need to
const urlParts = url.parse(req.url, true);
const someVar = urlParts.query.someVar;
//Below code shows how to get the request body if you need
let body = "";
req.on('data', Meteor.bindEnvironment(function (data) {
body += data;
}));
req.on('end', Meteor.bindEnvironment(function () {
//You can access complete request body data in the variable body here.
console.log("body : ", body);
//write code to save body in db or do whatever you want to do with body.
}));
//Below you send response from your webhook listener
res.writeHead(200);
res.end("OK");
});
关于Meteor - 创建一个 webhook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50320707/