I am writing expressjs app. is req.params.anything always string and not number suppose if I pass a number for user_id it's typeof is always string.
app.get('user/:user_id', function(req, res){
console.log(typeof req.params.user_id);
});
GET user/21
this logs string.
So is it always type string for req.params.x?
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. params property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /student/:id, then the “id” property is available as req.params.id.
Your query parameters can be retrieved from the query object on the request object sent to your route. It is in the form of an object in which you can directly access the query parameters you care about. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object.
params[:id] is meant to be the string that uniquely identifies a (RESTful) resource within your Rails application. It is found in the URL after the resource's name.
Yes, all params will be strings.
This is extracted from the expressjs route.js
:
var val = 'string' == typeof m[i]
? decodeURIComponent(m[i])
: m[i];
So the val
will always be a string, since the result of decodeURIComponent
is always a string, while m
is the result of a RegExp.exec() which returns an array of matched strings, so it's also safe to assume that m[i]
will be a string.
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