How to iterate over req.params of a POST request in Express.js

I am creating a general route handler for Post requests in node, and for this I need to iterate over req.params after the request, without knowing in advance what paramNames is. I tried the following without success:

console.log("checking param keys...")
Object.keys(req.param).forEach(function(key){
console.log(key +"is " + req.params(key) )
})

when I run this code, only "Checking key parameters ..." is printed. If the form was published with the "title" field with the value "monkey" and the "body" field with the value "rule", then the desired result will be: name - monkey body - this is the rule.

Does anyone know how to do this?

-edit: guess! thought the answer might be useful to others as well. I just needed to check req.body instead of req.param!

console.log("checking param keys...")
Object.keys(req.body).forEach(function(key){
console.log(key +"is " + req.params(key) )
})

Hope someone finds this helpful, I know what I did!

+3
1

, , POST, url, bodyParser() , .

req.params - , , -. . req.params, . , :

var app = require("express")();

app.use(express.bodyParser());
app.post("/form/:name", function(req, res) {
   console.log(req.params);
   console.log(req.body);
   console.log(req.query);
   res.send("ok");
});

:

$ curl -X POST --data 'foo=bar' http://localhost:3000/form/form1?url=/abc

:

[ name: 'form1' ]
{ foo: 'bar' }
{ url: '/abc' }

req.body - , req.query - HTTP-.

+3

All Articles