DevAcademy
LearnNode.jsQuery Params & Body Parsing
IntermediateNode.js

Query Params & Body Parsing

The different ways data arrives in a request, and how to read each one correctly.

Reading Time

12 min

Lesson

Lesson 22 of 34

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

LocationTypical Use
Route param (/users/:id)Identifying a specific resource
Query param (?sort=name&page=2)Optional filters, sorting, pagination
BodyData being created or updated, especially larger payloads

Reading Query Parameters

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Enabling and Reading a JSON Body

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Parsing Form-Encoded Bodies

Try it yourself — edit and run

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.

Interview Questions

Quick Quiz

1. What data type is a query parameter always received as?

2. What happens to req.body if express.json() is not registered?

3. Where would you typically put a resource's unique identifier in a request?