Andreas Zeiner
Andreas Zeiner

Reputation: 587

Nodes.js post parameters

1) Why is it that when i try to get parameters from a post request in node.js i get the correct values.

app.post('/users', function(req, res) {

  console.log(">>> " + req.param('name')); ' works but with warnings
  res.send("ok");


});

The above sample outputs a warning that this "param" function is deprecated and i should use params.name. using params.name is acutally not working and results in "undefined", the same with body.name.

this happens also when i use

router.post('/',function(req, res, next) {

  console.log("result > " + req.params.name); ' result is "undefined"
  res.send(req.body);
});

i've included bodyparser with json support.

Am i missing something or what exactly i'm doing wrong?

2) Furthermore is there a way to get all parameters without naming them? both for get/post requests?

Upvotes: 0

Views: 58

Answers (1)

Andreas Zeiner
Andreas Zeiner

Reputation: 587

after trying again and again i found it:

the post request may NOT be multipart encoded - so if enctype="multipart/form-data" is set, remove it or change it to "application/x-www-form-urlencoded"

after removing the enctype i tried to get the data with

var result = req.body.name

which worked.

for multipart encoded posts you've to use "multer" i guess!

EDIT

i just found out that

req.params.name

is only for urls parts e.g.

http://host:port/application/name

and the documentation clearly says that in express 4.x you've to use "use a multipart-handling middleware like busboy, multer, formidable, multiparty, connect-multiparty, or pez." (http://expressjs.com/api.html#router.route)

Upvotes: 2

Related Questions