2

我想用 Meteor 实现一个 webhook

我正在使用kadira/flowrouter Meteor 插件,但无法获取 POST 数据。queryParams 或 params 都不返回我想要获取的消息的正文。

FlowRouter.route('/my_weebhook', {
    action: function(params, queryParams) {
        console.log(queryParams);
        console.log(params);
    }
});
4

1 回答 1

2

我不知道如何使用 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");
  
});

于 2018-05-16T14:05:18.753 回答