Introduction
When a user sends a request to the server, the route handler function receives two parameters: the request object and the response object, which we usually write as req and res.
The req object contains all the data sent from the client side. This data can be present in different forms, such as req.body, req.params, req.query, and req.headers.
Most of the time, we work with req.body to handle incoming data, especially in POST or PUT requests. But there are two other important ways to receive data in Express.js: req.params and req.query.
req.params and req.query are both parts of the URL, but they work differently.
What URL Parameters Are?
URL parameters are generally a part of the URL path. They are used to uniquely identify or fetch a specific resource.
In Express.js, URL parameters are defined by adding : before the parameter name in the route path.
These parameters can then be accessed inside the route handler using req.params. The parameter name used in req.params must match the name defined in the URL.
app.get("/users/:id", (req, res) => {});
In the above code example you can see the :id which is using to fetch specific user by it's id.
What Query Parameters Are?
Query parameters are also a part of the URL, but their use case is different from URL parameters.
Query parameters are written at the end of the main URL path, after a ?. They are written as key-value pairs, where the key and value are separated by an = sign.
You can pass multiple query parameters by separating them with &.
Query parameters are generally used for filtering, sorting, searching, or pagination instead of identifying a specific resource.
/products?category=electronics&sort=price
Differences Between Params and Query
| Factor | req.params |
req.query |
|---|---|---|
| Position in URL | Part of the route path | Comes after ? in the URL |
| Purpose | Used to identify a specific resource | Used for filtering, sorting, searching, pagination |
| Syntax | Defined using : in route |
Written as key-value pairs |
| Required? | Usually required | Usually optional |
| Example URL | /users/101 |
/users?role=admin |
| Access in Express | req.params.id |
req.query.role |
| Multiple Values | Limited by route structure | Multiple parameters can be passed with & |
| Best Use Case | Fetching a single specific resource | Modifying or filtering results |
Accessing Params in Express
To access route parameters in Express.js, we can extract them from the req object using the req.params object.
There are two common ways to access them:
- Using dot notation — access the parameter directly by its name.
app.get("/users/:id", (req, res) => {
const userId = req.params.id;
res.send(userId);
});
- Using destructuring — extract the parameter into a variable.
app.get("/users/:id", (req, res) => {
const { id } = req.params;
res.send(id);
});
Accessing Query Strings in Express
In the same way, we can access query parameters in Express.js using the req.query object.
Just like route parameters, query parameters can also be accessed in two ways:
- Using dot notation — access the query value directly by its key.
app.get("/products", (req, res) => {
const category = req.query.category;
res.send(category);
});
- Using destructuring — extract the query values into variables.
app.get("/products", (req, res) => {
const { category } = req.query;
res.send(category);
});
When to Use Params vs Query
Params
Use route parameters (req.params) when you want to identify or access a specific resource. Route parameters are best when the value is required and directly represents the resource in the URL.
Examples:
/users/101→ get a specific user/products/55→ get a specific product/posts/10/comments/5→ get a specific comment of a post
Query
Use query parameters (req.query) when you want to modify, filter, or control the result without identifying a specific resource. Query parameters are usually optional.
Examples:
/users?role=admin→ filter users by role/products?category=electronics&sort=price→ filter and sort products/posts?page=2&limit=10→ pagination
Conclusion
Both params and query have their own use cases, so they should be used wisely to make your routes more meaningful and easier for other developers to understand.
Express.js makes handling the request and response objects very simple and flexible. Features like req.params and req.query make routing easier and help developers build clean and maintainable APIs with less effort.
I hope you enjoyed this blog and now have a clear understanding of the differences between params and query, and when to use each one.
