I'm using Node.js and I want to see all of the parameters that have been posted to my script. To get to my function, in my routes/index.js
I'm doing:
app.post('/v1/order', order.create);
Then in my function, I have:
exports.create = function(req, res, next) { console.log( req.params );
But it's returning an empty array. But when I do:
exports.create = function(req, res, next) { console.log( req.param('account_id') );
I get data. So I'm a bit confused as to what's going on here.
The req. params property is an object that contains the properties which are mapped to the named route "parameters". For example, if you have a route as /api/:name, then the "name" property is available as req.params.name. The default value of this object is {}.
# req. param() searches the URL path, body, and query string of the request (in that order) for the specified parameter. If no parameter value exists anywhere in the request with the given name , it returns undefined or the optional defaultValue if specified.
params is an object of the req object that contains route parameters. If the params are specified when a URL is built, then the req. params object will be populated when the URL is requested.
The req. param() function basically returns the value of param name when present. When parameters are passed, then it can be accessed using this function. Parameter: The name parameter is the name of the parameter.
req.params
can only get the param of request url in this pattern:/user/:name
req.query
get query params(name) like /user?name=123
or body params.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With