Three Ways Data Arrives
A request can carry data in its URL path (route params), its query string (query params), or its body — each suited to a different kind of data.
When to Use Each
| Location | Typical Use |
|---|---|
| Route param (/users/:id) | Identifying a specific resource |
| Query param (?sort=name&page=2) | Optional filters, sorting, pagination |
| Body | Data being created or updated, especially larger payloads |
Reading Query Parameters
Console Output
Click “Run” to see the console output here.
Enabling and Reading a JSON Body
Console Output
Click “Run” to see the console output here.
Parsing Form-Encoded Bodies
Console Output
Click “Run” to see the console output here.
req.body is undefined Without Middleware
Without express.json() (or a similar body-parsing middleware) registered before the route, req.body is undefined even if the client sent a valid JSON body — a very common source of confusion for beginners.
Best Practice
Query parameters are always strings, even ones that look numeric — always explicitly convert them (Number(req.query.page)) rather than assuming JavaScript will coerce them correctly in every comparison.