# Query Strings — Express.js

Source: https://www.geekswithgeeks.com/en/expressjs/ex-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`.

```javascript
app.get('/search', (req, res) => {
  const { q, page = '1' } = req.query;
  res.json({ query: q, page: Number(page) });
});
```

**Quiz:** For `GET /users/5?active=true`, where does `5` end up?

- [ ] req.query.id
- [x] 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`.
