Difference Between Params and Query in URL (and React/Express)
🔹 1. Params (Route Parameters):
- Part of the URL path
- Used to identify a specific resource
- Defined with
:in route (e.g.,/user/:id) - Accessed using
req.paramsin Express
Example:
// URL: /user/42
<Route path="/user/:id" element={<User />} />
In backend (Express):
app.get('/user/:id', (req, res) => {
console.log(req.params.id); // 42
});
🔹 2. Query Parameters:
- Appended to the URL after
? - Used for filtering, sorting, or searching
- Multiple key-value pairs with
& - Accessed using
req.queryin Express
Example:
// URL: /search?term=react&page=2
In backend:
app.get('/search', (req, res) => {
console.log(req.query.term); // react
console.log(req.query.page); // 2
});
✅ Summary:
- Params are part of the route and usually identify resources.
- Query is for optional filters or metadata, not part of route structure.