Skip to main content

Command Palette

Search for a command to run...

URL Parameters vs Query Parameters in Express.js

Updated
5 min read
URL Parameters vs Query Parameters in Express.js

A Complete Guide with Practical Examples and Real-World Usage


Introduction

When building APIs or web applications, passing data through URLs is a common requirement. Whether you are fetching a specific user or applying filters to a search, understanding how URLs carry information is essential.

Two key ways to pass data in URLs are:

  • URL Parameters (Route Params)

  • Query Parameters (Query Strings)

Although they may look similar, they serve different purposes and are used in different scenarios.

This blog explains both concepts in depth, how to use them in Express.js, and when to use each.


What URL Parameters Are

URL parameters (also called route parameters) are dynamic values embedded directly within the URL path.

Key Idea

They act as identifiers for specific resources.


Example

/users/123

Here:

  • /users → resource

  • 123 → parameter (user ID)


Why They Are Used

URL parameters are used when you want to:

  • Access a specific resource

  • Identify something uniquely


Real-World Example

GET /products/45
  • Fetch product with ID 45

Express Example

app.get("/users/:id", (req, res) => {
  const userId = req.params.id;
  res.send(`User ID is ${userId}`);
});

Important Note

  • Defined using : in route

  • Extracted using req.params


What Query Parameters Are

Query parameters are key-value pairs appended to the URL after a ?.

Key Idea

They act as filters, modifiers, or optional data.


Example

/products?category=electronics&price=1000

Here:

  • category=electronics → filter

  • price=1000 → filter


Why They Are Used

Query parameters are used when you want to:

  • Filter results

  • Sort data

  • Modify response


Real-World Example

GET /users?age=25&city=Delhi
  • Fetch users filtered by age and city

Express Example

app.get("/users", (req, res) => {
  const { age, city } = req.query;
  res.send(`Age: \({age}, City: \){city}`);
});

Important Note

  • Comes after ? in URL

  • Multiple params separated by &

  • Accessed using req.query


URL Structure Breakdown

http://example.com/users/123?active=true
        │        │        │
        │        │        └── Query Parameter
        │        └────────── URL Parameter
        └────────────────── Base URL

Differences Between URL Params and Query Params

Feature URL Parameters Query Parameters
Location Path After ?
Purpose Identify resource Filter/modify data
Required Usually required Usually optional
Example /users/1 /users?age=20

Accessing Params in Express

Express makes it easy to access URL parameters.


Example

app.get("/products/:id", (req, res) => {
  const productId = req.params.id;
  res.send(`Product ID: ${productId}`);
});

Multiple Params

app.get("/users/:userId/orders/:orderId", (req, res) => {
  const { userId, orderId } = req.params;
  res.send(`User: \({userId}, Order: \){orderId}`);
});

Accessing Query Strings in Express

Query parameters are accessed using req.query.


Example

app.get("/search", (req, res) => {
  const { keyword, page } = req.query;
  res.send(`Search: \({keyword}, Page: \){page}`);
});

Example URL

/search?keyword=node&page=2

When to Use Params vs Query

Choosing between params and query depends on your use case.


Use URL Params When

  • You need a specific resource

  • The value is required

  • It represents identity

Example

/users/123

Use Query Params When

  • You are filtering or sorting

  • Data is optional

  • You want flexibility

Example

/products?category=books&sort=price

Practical Examples


1. User Profile (Params)

GET /users/101
  • Fetch user with ID 101

2. Search Filters (Query)

GET /products?category=shoes&size=9
  • Filter products

3. Combined Usage

GET /users/101/posts?limit=10
  • Params → identify user

  • Query → limit results


Params vs Query Comparison Diagram

URL PARAMS:
 /users/123
        ↑
   Identifier

QUERY PARAMS:
 /users?age=25
        ↑
   Filter/Modifier

Common Mistakes to Avoid

  • Using query instead of params for IDs

  • Making required data optional via query

  • Overloading URLs with too many query params


Best Practices

  • Keep URLs clean and readable

  • Use params for identity

  • Use query for filtering

  • Maintain consistency in API design


Key Takeaways

  • URL params identify resources

  • Query params modify or filter data

  • Both are accessed differently in Express

  • Choosing the right one improves API design


Conclusion

Understanding the difference between URL parameters and query parameters is essential for designing clean and effective APIs. While params are used to uniquely identify resources, query parameters provide flexibility for filtering and modifying results.

Mastering this distinction helps you build APIs that are intuitive, scalable, and easy to use in real-world applications.