Facebook Pixel

Route Params vs Query Params vs Body in Express

Express exposes three ways to send data: params, query, and body. Here is when to use each.

Route Params vs Query Params vs Body in Express

Express gives you three ways to read data from a request: params, query, and body. Here is when to use each.

Route Params

Used to identify a specific resource. Part of the URL path.

GET /users/:id
req.params.id

Use for: identifying a single resource (a user id, a post id). Always a string. Convert to ObjectId or number when needed.

Query Params

Used for optional filters and options. After the ? in the URL.

GET /users?role=admin&limit=10
req.query.role, req.query.limit

Use for: filtering, sorting, pagination, optional fields. Always strings. Coerce when needed.

Body

Used for the data you are sending to create or update a resource. Sent in the request body (usually JSON).

POST /users { "name": "Kunal", "email": "[email protected]" }
req.body.name, req.body.email

Use for: POST and PATCH bodies. Requires express.json middleware. The body can be large and complex.

Comparison

AspectRoute ParamsQuery ParamsBody
Identifies a resource?YesNoNo
Optional?No (required by path)YesDepends
LengthShortShortLong
Cached in URL?YesYesNo
MethodAnyGET mostlyPOST, PUT, PATCH

When to Use Each

  • Route params: when the data identifies a resource. /users/:id. Required.
  • Query params: when the data is optional filters. /users?role=admin. Optional.
  • Body: when the data is large or complex, or you are creating/updating. POST /users with body.

Common Mistakes

  • Putting IDs in query params (should be in path).
  • Putting filters in the body of a GET request (GET should not have a body).
  • Forgetting to add express.json, so req.body is undefined.

The Takeaway

Route params identify a resource (in path, required). Query params filter (after ?, optional). Body sends data for create/update (with express.json, larger). Use the right one for the right job. Do not put IDs in query params or filters in body.

Route params identify a resource (in the path, required). Query params filter (after ?, optional). Body sends data for create/update (with express.json, larger). Use each for the right job.

When the data identifies a specific resource. /users/:id where id is required. Always a string. Convert to ObjectId or number when needed.

For optional filters, sorting, and pagination. /users?role=admin&limit=10. Always strings. Coerce when needed. Optional, so the endpoint works without them too.

For POST and PATCH where you are creating or updating a resource. Requires express.json middleware. The body can be large and complex. Do not put filters in body of a GET request.

Putting IDs in query params (should be in the path) or putting filters in the body of a GET request (GET should not have a body). Also forgetting express.json so req.body is undefined for POSTs.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.