流星 - 创建一个webhook

时间:2018-05-13 21:17:23

标签: meteor kadira

我想用Meteor实现webhook

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

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

1 个答案:

答案 0 :(得分:1)

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