req.query contain?/search?term=book&sort=asc, what will req.query contain?The req.query object contains the parsed query string parameters as key-value pairs. For the URL /search?term=book&sort=asc, req.query will be { term: 'book', sort: 'asc' }.
/items?category=tools, which code correctly accesses the category query parameter in Express?app.get('/items', (req, res) => {
// Access category here
});Query parameters are accessed via req.query. Route parameters use req.params, and POST data uses req.body.
req.query.page undefined?page. The URL is /list?page=2. The code is:app.get('/list', (req, res) => {
const page = req.query.page;
res.send(`Page number is ${page}`);
});But the response shows
Page number is undefined. What is the likely cause?If the URL does not contain a question mark before query parameters, req.query will be empty. The URL must be /list?page=2, not /listpage=2.
app.get('/filter', (req, res) => {
const { type = 'all', limit = 10 } = req.query;
res.json({ type, limit });
});What will be the JSON response for the URL
/filter?limit=5?Query parameters are strings, so limit is the string "5". The default type is used because it was not provided.
/search?tag=js&tag=node&tag=express, what will req.query.tag contain in Express by default?By default, Express's query parser returns the last value for repeated keys. So req.query.tag will be the string "express".
