How to access the GET parameters after "?" in Express?
Solution 1
So, after checking out the express reference, I found that req.query.color
would return me the value I'm looking for.
req.params refers to items with a ':' in the URL and req.query refers to items associated with the '?'
Example:
GET /something?color1=red&color2=blue
Then in express, the handler:
app.get('/something', (req, res) => {
req.query.color1 === 'red' // true
req.query.color2 === 'blue' // true
})
Solution 2
Use req.query, for getting he value in query string parameter in the route. Refer req.query. Say if in a route, http://localhost:3000/?name=satyam you want to get value for name parameter, then your 'Get' route handler will go like this :-
app.get('/', function(req, res){
console.log(req.query.name);
res.send('Response send to client::'+req.query.name);
});
Solution 3
Update: req.param()
is now deprecated, so going forward do not use this answer.
Your answer is the preferred way to do it, however I thought I'd point out that you can also access url, post, and route parameters all with req.param(parameterName, defaultValue)
.
In your case:
var color = req.param('color');
From the express guide:
lookup is performed in the following order:
- req.params
- req.body
- req.query
Note the guide does state the following:
Direct access to req.body, req.params, and req.query should be favoured for clarity - unless you truly accept input from each object.
However in practice I've actually found req.param()
to be clear enough and makes certain types of refactoring easier.
Solution 4
Query string and parameters are different.
You need to use both in single routing url
Please check below example may be useful for you.
app.get('/sample/:id', function(req, res) {
var id = req.params.id; //or use req.param('id')
................
});
Get the link to pass your second segment is your id example: http://localhost:port/sample/123
If you facing problem please use Passing variables as query string using '?' operator
app.get('/sample', function(req, res) {
var id = req.query.id;
................
});
Get link your like this example: http://localhost:port/sample?id=123
Both in a single example
app.get('/sample/:id', function(req, res) {
var id = req.params.id; //or use req.param('id')
var id2 = req.query.id;
................
});
Get link example: http://localhost:port/sample/123?id=123
Solution 5
@Zugwait's answer is correct. req.param()
is deprecated. You should use req.params
, req.query
or req.body
.
But just to make it clearer:
req.params
will be populated with only the route values. That is, if you have a route like /users/:id
, you can access the id
either in req.params.id
or req.params['id']
.
req.query
and req.body
will be populated with all params, regardless of whether or not they are in the route. Of course, parameters in the query string will be available in req.query
and parameters in a post body will be available in req.body
.
So, answering your questions, as color
is not in the route, you should be able to get it using req.query.color
or req.query['color']
.
Hanfei Sun
Just another stackoverflow user cs.cmu.edu/~hanfeis
Updated on September 13, 2021Comments
-
Hanfei Sun over 2 years
I know how to get the params for queries like this:
app.get('/sample/:id', routes.sample);
In this case, I can use
req.params.id
to get the parameter (e.g.2
in/sample/2
).However, for url like
/sample/2?color=red
, how can I access the variablecolor
?I tried
req.params.color
but it didn't work. -
Arj 1411 over 7 yearsCould you please tell me how to validate "id" ?
-
Jochem Schulenklopper about 7 years@AnandRaj: what do you mean with: how to validate "id"? What kind of validation do you want? BTW, you can get the value of
id
in your function like this:var sampleId = req.params.id;
. -
Schuere almost 7 yearsperhaps some info about querystring to get a complete answer
-
adelriosantiago over 6 yearsUse
req.params.whatever
in latest versions. -
sdgfsdh over 6 yearsThis is probably a bad idea because it makes it harder to maintain your endpoints. You no longer know which method clients will be using to pass parameters.
-
Lee Brindley over 6 yearsThat is actually one of the main advantages of this approach to be honest, not having to know where the fields come from. The ExpressData class above acts as a bridge, allowing you to modularise your business logic, moving it away from the express controller routes, i.e. you're not baking 'req.query', 'req.body' into your code, this also makes your business code easily testable, completely outside of express.
-
iBug over 6 yearsThank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
-
Bruno Tavares about 6 yearsThanks this answer was very helpful!
-
caesarsol over 5 yearsMind that
req.params
is different fromreq.query
! expressjs.com/en/api.html#req.params expressjs.com/en/api.html#req.query @adelriosantiago -
Andy Lorenz almost 4 yearsI don't believe this answer (from the OP) actually answers his own original question! He clearly asked how to access a querystring value IN COMBINATION WITH A POSITIONAL PARAMETER (:id). I have exactly the same issue, and this answer does NOT provide a solution ?!