Lesson 6 / 23
Query Strings
Optional key=value pairs after the ? in a URL.
req.query
Everything after ? in the URL — filters, pagination, search terms — lands in req.query as an object of strings.
Reading query params
GET /search?q=express&page=2 gives both values on req.query.
app.get('/search', (req, res) => {
const { q, page = '1' } = req.query;
res.json({ query: q, page: Number(page) });
});Quick check: For `GET /users/5?active=true`, where does `5` end up?
- req.query.id
- req.params.id (if the route is /users/:id)
- req.body.id
Answer
req.params.id (if the route is /users/:id) — Path segments captured with `:name` go to `req.params`; only text after `?` goes to `req.query`.