Easy way to handle post data in meteor.js?
Asked Answered
T

4

6

I need to handle some POST data in my meteor.js app, is there an easy way to do this?

Very basic, if it was a PHP app I would just want the $_POST variable.

Tatterdemalion answered 4/2, 2013 at 9:28 Comment(2)
One could argue that it's not best practice to actually do POSTs in a meteor app, at least to your meteor server. If your want to do HTTP request to a remote server have a look here: docs.meteor.com/#meteor_httpHundredth
Yeah, it's an external service that will post data to the app, nothing I can do about that in this case I'm afraid. :( So if it's possible it would be super.Tatterdemalion
C
4

Meteor router

https://github.com/tmeasday/meteor-router#server-side-routing

Meteor.Router.add('/items/:id', 'POST', function(id) {
  // update Item Function
  return [200, 'ok'];
});
Cuticula answered 5/2, 2013 at 1:53 Comment(3)
What I would need though is to access the POST data on the client, is that possible using the meteor router?Tatterdemalion
no.. on the client you can only use params and query, no bodyCuticula
@KristofferK you can use Meteor.publish() and Meteor.subscribe() to send POST data from the server to the client.Exultation
E
1

If you are simply looking to intercept the GET and POST data, then send Meteor on it's merry way, you could do something like this on the server.

if (Meteor.isServer) {

  var connect = Npm.require('connect');
  var app = __meteor_bootstrap__.app;
  var post, get;

  app
    // parse the POST data
    .use(connect.bodyParser())
    // parse the GET data
    .use(connect.query())
    // intercept data and send continue
    .use(function(req, res, next) {
      post = req.body;
      get = req.query;
      return next();
    });

  Meteor.startup(function() {
    // do something with post and get variables
  });

}

EDIT 11/01/13

I ended up creating a smart package for this (for myself). There is no documentation but you are welcome to use it. https://github.com/johnnyfreeman/request-data

To retrieve the foo request variable:

RequestData.get('foo') // --> 'bar'
RequestData.post('foo') // --> 'bar'

Both methods will throw a Meteor.Error if the key isn't found so make sure you use wrap with a try/catch if the variable is optional.

Exultation answered 2/8, 2013 at 13:6 Comment(1)
for others trying this, 'connect' isn't included in Meteor, you'll need to install via NPM or try the custom package (which looks like it installs)Uncanonical
L
1

I'm using this package to serialize body data: simple:json-routes. Here is the link.

And this code snippet to access it:

WebApp.connectHandlers.use('/api/request', (req, res, next) => {
    console.log(req.body);
});
Legionary answered 8/9, 2021 at 9:34 Comment(0)
L
0

You can use Meteor's Iron Router, docs here, since Router (as mentioned above) is outdated and might be no longer functional.

Router.route('/items/:id', {where: 'server'})
    .get(function () {
        this.response.end('get request\n');
    })
    .post(function () {
        this.response.end('post request\n');
    });
Lane answered 4/5, 2016 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.