100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Node.js

Route Parameters and Query Strings

Understand how to capture dynamic URL segments and optional query data in Express routes.

Building APIsBeginner9 min readJul 8, 2026
Analogies

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

javascript
// 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

javascript
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

Was this page helpful?

Topics covered

#NodeJs#NodeJsExpressStudyNotes#WebDevelopment#RouteParametersAndQueryStrings#Route#Parameters#Query#Strings#SQL#StudyNotes#SkillVeris