Receiving post request with a little help from community

Hello guys, today I’ll write about receiving POST requests with Meteor, not because it’s not possible or because there aren’t other ways to do that but, because of some packages that I’ve found doing that.

 

So, last week I had to write an entry point on my app to receive post callbacks and obviously it could not interfere and could not be treated by FlowRouter so, after some research I found many solutions but the simple one was using Picker, a meteorhacks package.

 

Ok, I write an entry point and started to receive my post request but, Meteor requests are actually Node.js incoming messages and guess what? I was not able to catch the sent parameters easily, I realized I needed some help and I found node-formidable, amazing package, It provides a simple way to get those parameters. That’s ok, I’m fine now right? Wrong!

 

With Meteor you can use npm modules just inside packages, you can’t just use it directly on your Meteor apps but, guess what, there’s a way and again arunoda comes to save the day! He wrote a package to make it possible it’s just add meteorhacks:npm to use npm packages directly.

So, in the end of day, with a little help of those incredible packages, I had my small entry point working nicely.

 

 

[code language=”javascript”]
var formidable = Meteor.npmRequire(‘formidable’);

Picker.route(‘/status/transacoes/:posbackId’, function(params, req, res, next) {
var posbackId = params.posbackId;

if (req.method == ‘POST’) {
var form = new formidable.IncomingForm();

form.parse(req, Meteor.bindEnvironment(function (err, fields) {

var status = fields[‘current_status’];

Transactions.update({posbackId:posbackId},{$set:{status:status}})

}));

}

var status = {
posbackId:posbackId,

status:"received"

}

res.writeHead(200, {‘Content-Type’: ‘application/javascript’});

res.end(JSON.stringify(status),’utf8′);

});
[/code]

 

That’s the importance of community, that’s why I try to contribute even with this simple blog, so, thank you again meteorhacks, thank you again arunoda!

 

https://github.com/meteorhacks/picker

https://nodejs.org/api/http.html#http_http_incomingmessage

https://github.com/felixge/node-formidable

https://github.com/arunoda

https://github.com/meteorhacks/npm

 

Leave a Reply