soumced
soumced

Reputation: 1

intercept data posted in meteorjs using extern api

I am using meteor-react, and I encounter some problems with the external api. In my project, I use pure meteor as server and react as client.

I want to get some data posted using a url's body (sent from external api).

ex: this an example of the url with the body

http:\\api\test
{
  name: test
}

I need to get the information in the body.

I really don't know how to implement that in my meteor project, and i don't know from where (whether the server side or client side), I should implement my code.

I have tried these instructions but it is not working:

WebApp.connectHandlers.use('/notif',bodyParser.json());
WebApp.connectHandlers.use('/path', bodyParser.urlencoded());
WebApp.connectHandlers.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  API.handleRequest(res, req);
});

Thanks in advance for your help

Upvotes: 0

Views: 82

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21384

What you are looking for is a server-side router. I would recommend https://atmospherejs.com/meteorhacks/picker since it is more light-weight than the default alternative, iron-router.

Add the package to your project:

meteor add meteorhacks:picker

Add the body-parser npm package:

meteor npm install --save body-parser

In your server code:

const bodyParser = require('body-parser');
Picker.middleware(bodyParser());

Picker.route('/api/test', (params, req, res, next) => {
  console.log("tada!", req.body.name);
  res.end('ok');
});

Upvotes: 0

Related Questions