Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to handle post data in meteor.js?

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.

like image 581
Kristoffer K Avatar asked Feb 04 '13 09:02

Kristoffer K


2 Answers

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'];
});
like image 63
Suburbio Avatar answered Nov 01 '22 07:11

Suburbio


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.

like image 1
Johnny Avatar answered Nov 01 '22 07:11

Johnny