Meteor - 创建一个 webhook

标签 meteor kadira

我想用 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/

相关文章:

meteor - 为什么 Meteorite 每次都说 "smart.json changed.."?

javascript - meteor :使外部脚本的参数具有反应性

javascript - 在 Meteor 模板中嵌入 Javascript 小部件

meteor - 如何使 minimongo.js 在浏览器中工作?

meteor - 如何在 meteor 中包含来自CDN的JavaScript?

meteor - 如何使用 blaze-layout 渲染将数据传递到模板?

mongodb - 登录期间长时间的 observeChanges 调用