Introduction
Real-world APIs need to accept dynamic input from the URL. Express supports two main mechanisms for this: route parameters, which capture named segments of the URL path, and query strings, which capture optional key-value pairs appended after a question mark. Knowing when to use each is essential for designing clean, functional endpoints.
Cricket analogy: Route parameters are like specifying a fixed seat number in the stadium (/section/A/seat/12), always pointing to one exact spot, while query strings are like optional add-ons such as '?withCushion=true' that modify the experience without changing which seat you're in.
Syntax
// Route parameter
app.get('/users/:id', (req, res) => {
console.log(req.params.id);
});
// Query string
app.get('/search', (req, res) => {
console.log(req.query.q, req.query.page);
});Explanation
Route parameters are defined with a colon prefix (:id) and are accessed via req.params. They are best used for identifying a specific resource, such as /users/42. Query strings, accessed via req.query, are best used for optional filtering, sorting, pagination, or search terms, such as /products?category=books&sort=price. Express can also define multiple route parameters and even optional ones using a question mark suffix in Express 4 (e.g. /files/:name?).
Cricket analogy: req.params identifies a specific innings like /matches/42 the way a scorecard entry names one exact match, while req.query handles optional filters like /players?team=india&sort=runs the way a stats page lets you filter and sort without pinning to one match.
Example
app.get('/users/:userId/orders/:orderId', (req, res) => {
const { userId, orderId } = req.params;
res.json({ userId, orderId });
});
app.get('/products', (req, res) => {
const { category, sort = 'name', page = 1 } = req.query;
res.json({ category, sort, page: Number(page) });
});Output
A request to GET /users/7/orders/99 returns { "userId": "7", "orderId": "99" }. A request to GET /products?category=books returns { "category": "books", "sort": "name", "page": 1 }, showing that req.query values not provided fall back to defaults defined via destructuring.
Cricket analogy: GET /matches/7/innings/99 returns { "matchId": "7", "inningsId": "99" } like a scorecard lookup pinpointing exact match and innings; GET /players?team=india returns { "team": "india", "sort": "name", "page": 1 } showing unspecified query values fall back to sensible defaults.
Key Takeaways
- Route parameters (req.params) identify a specific resource within the URL path.
- Query strings (req.query) carry optional data like filters, sorting, and pagination.
- All req.params and req.query values arrive as strings and may need type conversion.
- Multiple route parameters can be combined in a single path, e.g. /users/:userId/orders/:orderId.
- Default values for query parameters can be set using destructuring defaults.
Practice what you learned
1. In the route '/users/:id', how is the value of :id accessed inside the handler?
2. For the request GET /search?q=node&page=2, what is req.query.page?
3. Which type of URL data is most appropriate for optional filtering, like sorting a product list?
4. Which route definition correctly captures two route parameters?
Was this page helpful?
You May Also Like
RESTful API Design
Learn the core principles of REST: resource-based URLs, proper HTTP methods, and meaningful status codes.
Routing in Express
Understand how Express matches HTTP methods and URL paths to handler functions using routes.
Request and Response Objects in Express
Explore the key properties and methods of Express's req and res objects used to read requests and craft responses.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics