Skip to main content

Command Palette

Search for a command to run...

Understanding URL Parameters and Query Parameters in Express.js

Updated
5 min read
Understanding URL Parameters and Query Parameters in Express.js

When building backend APIs with Express.js, one of the most important concepts is understanding how data travels through URLs.

Almost every application uses URLs like:

/users/101

or

/products?category=mobile&sort=price

Although both URLs pass data to the backend, they work differently.

Understanding the difference between URL parameters and query parameters is extremely important for building clean and scalable APIs.

In this article, we will understand:

  • What URL parameters are

  • What query parameters are

  • Differences between params and query strings

  • How to access them in Express

  • When to use params vs query

  • Real-world backend examples


Understanding URL Structure

Consider this URL:

http://localhost:3000/users/101?active=true

This URL contains multiple parts.


URL Structure Breakdown

http://localhost:3000/users/101?active=true
                         |       |
                         |       |
                      Params   Query
  • /users/101 → URL parameter

  • ?active=true → Query parameter

Both are used differently in backend systems.


What Are URL Parameters?

URL parameters are dynamic values inside the route path.

They are mainly used to identify a specific resource.

Example:

/users/101

Here:

101

is the URL parameter.

Usually, this represents:

  • User ID

  • Product ID

  • Order ID

  • Post ID

URL params are commonly used for unique identification.


Express Route with URL Params

Example:

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

If the request is:

/users/101

Output:

User ID: 101

Understanding :id

In Express:

:id

means:

"Dynamic value will come here."

Express automatically extracts that value into:

req.params

Accessing Params in Express

Example:

app.get("/products/:productId", (req, res) => {
  console.log(req.params);

  res.send(req.params);
});

Request:

/products/500

Output:

{
  "productId": "500"
}

Params as Identifiers

URL params are best understood as identifiers.

They usually represent:

  • One specific user

  • One product

  • One resource

  • One entity

Example routes:

/users/10
/posts/45
/orders/999

Each route points to a unique resource.


What Are Query Parameters?

Query parameters are optional values added after ? in the URL.

Example:

/products?category=mobile&sort=price

Here:

  • category=mobile

  • sort=price

are query parameters.

Query parameters are usually used for:

  • Filtering

  • Searching

  • Sorting

  • Pagination

  • Modifiers


Query Parameter Structure

/products?category=mobile&sort=price
          |                 |
          |                 |
       Query Key         Query Key

Accessing Query Parameters in Express

Example:

app.get("/products", (req, res) => {
  console.log(req.query);

  res.send(req.query);
});

Request:

/products?category=mobile&sort=price

Output:

{
  "category": "mobile",
  "sort": "price"
}

Express automatically stores query parameters inside:

req.query

Query as Filters or Modifiers

Query parameters usually modify results instead of identifying resources.

Example:

/products?category=mobile

This means:

"Give me products filtered by category."

Another example:

/users?page=2

This means:

"Give me page 2 users."

Real-World Example: User Profile

URL params example:

/users/101

Meaning:

Fetch specific user with ID 101

This identifies a unique resource.


Real-World Example: Search Filters

Query params example:

/products?category=laptop&brand=lenovo

Meaning:

Filter products by category and brand

This modifies search behavior.


Params vs Query Comparison

Feature URL Params Query Params
Purpose Identify resource Filter or modify results
Position Inside route path After ?
Required Usually required Usually optional
Example /users/10 ?page=2
Access Method req.params req.query
Best For IDs and unique resources Search, filtering, pagination

Params vs Query Visualization

URL PARAM

/users/101
        |
        V
   Resource Identifier


QUERY PARAM

/products?category=mobile
           |
           V
      Filter / Modifier

Combining Params and Query

Both can work together.

Example:

/users/101/orders?status=completed

Meaning:

  • 101 → User identifier

  • status=completed → Filter

Express example:

app.get("/users/:id/orders", (req, res) => {
  console.log(req.params.id);
  console.log(req.query.status);

  res.send("Orders fetched");
});

When to Use URL Params

Use params when:

  • Resource identity is required

  • Route represents a unique entity

  • The value is mandatory

Examples:

/users/10
/products/500
/orders/999

When to Use Query Parameters

Use query parameters when:

  • Filtering data

  • Searching

  • Sorting

  • Pagination

  • Optional modifiers

Examples:

/products?category=mobile
/users?page=2
/search?q=laptop

Common Beginner Mistakes

Using Query for Resource IDs

Bad example:

/user?id=10

Better:

/users/10

Because the ID identifies a unique resource.


Using Params for Filters

Bad example:

/products/mobile/price

Better:

/products?category=mobile&sort=price

Filters belong in query strings.


Common Interview Questions

Difference Between Params and Query

Very common Express.js interview question.


Why Use Params for IDs?

Because params represent resource identity.


Why Use Query for Filtering?

Because query parameters modify results instead of identifying resources.


Real-World API Examples

GitHub

/users/gagansharmagit

Uses params.


E-commerce APIs

/products?category=shoes&page=2

Uses query parameters.