Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

req.params.number is string in expressjs?

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?

like image 864
Yalamber Avatar asked Aug 05 '13 12:08

Yalamber


People also ask

What is req params ID in Express?

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.

What is the type of REQ params?

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.

How do I get query params Express?

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.

What is params ID?

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.


1 Answers

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.

like image 111
randunel Avatar answered Sep 24 '22 19:09

randunel