How to access the GET query parameters in Express
Learn, how to access the query parameters from a url in express.
Query parameters are passed at the end of a url using a question mark(?) followed by the key-value pairs.
Example:
localhost:3000/users?name=gowtham
In this url, the key is name
and value is gowtham
Multiple query parameters can be passed by using a &
(And operator).
localhost:3000/users?name=gowtham&id=1234
Accessing query parameters
To access the query parameters in an express route handler, we need to use req.query
object.
const express = require('express');
const app = express();
app.get('/users', (req, res) => {
const name = req.query.name; const id = req.query.id; res.send(`User is ${name}, id is ${id}`);
});
app.listen(3000, () => console.log(`App is running`));