Picker - How to get POST params?


#1

I have the following code:

import { Picker } from 'meteor/meteorhacks:picker';

Picker.route('/trackupdate', function (params, req, res) {
  console.log(params);
  res.end('OK');
});

params doesn’t contain any POST variables, and req.body is undefined. How do I get POST data?


#2

That works for me

Picker.route(route, function(params, req, res) {
  if (req.method == 'POST') {
    var body = '';
    req.on('data', function (data) {
      body += data;
      // Too much POST data, kill the connection!
      // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
      if (body.length > 1e6)
        req.connection.destroy();
    });
    req.on('end', function () {
      req.body = JSON.parse(body);
      doSomething(params, req, res);
    });
  }
});

#3

Got it! body-parser was the missing key. I’m sending { magic: 'works' } to the server.

import { Picker } from 'meteor/meteorhacks:picker';
import bodyParser from 'body-parser';

Picker.middleware(bodyParser.urlencoded({ extended: false }));
Picker.middleware(bodyParser.json());

const postRoutes = Picker.filter((req, res) => {
  return req.method === 'POST';
});

postRoutes.route('/trackupdate', function (params, req, res) {
  console.log(req.body.magic);
  res.end('OK');
});

#4

much cleaner than mine


#5

The less my code can look like “raw” Node.js, the better. :wink: