Meteor - Create a webhook

855 views Asked by At

I'd like to implement a webhook with Meteor

I'm using the kadira/flowrouter Meteor plugin but no way to get POST data. Neither queryParams or params return the body of the message that I want to get.

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

There are 1 answers

0
Kapil On

I do not know how to use kadira/flowrouter but you can use Meteor base package WebApp

to achieve what you need. Below is a sample code. You need to code or import something like this in your 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");
  
});