In Express.js all the query parameters are stored in the object req.query
, so you can
access them in any of your route handlers or middleware functions.
So imagine your Express backend is running on https://learn.coderslang.com
and your client tries to GET https://learn.coderslang.com?id=125&name=hero
const routeHandler = (req, res, next) => {
console.log(req.query.id); // 125
console.log(req.query.name); // hero
console.log(req.query.surname); // undefined
next();
}
In the example above I accessed the query string variables id
and name
and logged them to the
screen. The parameter surname
is undefined
as it’s missing from the query string.