# Understanding URL Parameters and Query Parameters in Express.js 

When building backend applications or APIs, the URL does much more than just open a webpage.

It can also carry information.

Sometimes the information is used to identify a specific resource. Other times, it is used to filter or modify the result.

This is where:

*   URL parameters
    
*   Query parameters
    

come into the picture.

At first, they look very similar because both send data through the URL. But their purpose is completely different.

Understanding this difference is important because almost every real-world API uses them.

* * *

### Understanding the URL Structure First

Before learning params and query strings separately, let’s first break down a URL.

Example:

/users/101?sort=asc

This URL has two parts:

*   `/users/101` → path section
    
*   `?sort=asc` → query section
    

The path usually identifies *what resource we want*.

The query section usually modifies *how we want the result*.

That is the core difference.

* * *

### What Are URL Parameters?

URL parameters are values written directly inside the route path.

They are mainly used to identify a specific resource.

Example:

/users/101

Here:

*   `users` → resource type
    
*   `101` → specific user ID
    

So this URL basically means:

👉 “Give me the user whose ID is 101.”

* * *

### URL Parameters in Express

In Express.js, URL parameters are defined using `:`.

Example:

app.get("/users/:id", (req, res) => {

console.log([req.params.id](http://req.params.id))

})

If the user visits:

`/users/101`

Output:

`101`

Express automatically stores route parameters inside:

req.params

* * *

### Why URL Parameters Exist

Imagine websites without URL parameters.

You would need separate routes like:

`/user1`

`/user2`

`/user3`

That would be terrible.

Instead, parameters make routes dynamic.

One route can handle thousands of users:

`/users/:id`

This makes backend systems scalable and reusable.

* * *

### Real-World Examples of Params

**User Profile**

`/users/25`

Meaning: Fetch user whose ID is 25

* * *

**Blog Post**

`/posts/10`

Meaning: Fetch blog post with ID 10

* * *

**Product Details**

`/products/501`

Meaning: Fetch product 501

* * *

### What Are Query Parameters?

Query parameters are written after `?` in the URL.

They are mostly used for:

*   filtering
    
*   searching
    
*   sorting
    
*   pagination
    
*   optional settings
    

Example:

`/products?category = shoes&sort = price`

Here:

*   `category=shoes`
    
*   `sort=price`
    

are query parameters.

Unlike params, they usually do not identify a single resource.

They modify the response.

* * *

### Query Parameters in Express

Express stores query strings inside:

`req.query`

Example:

app.get("/products", (req, res) => {

console.log(req.query)

})

URL:

`/products?category=shoes&sort=price`

Output:

{

category: "shoes",

sort: "price"

}

* * *

### Why Query Parameters Exist

Imagine an online shopping site.

Without query parameters, filtering would become impossible.

Example:

`/products?brand=nike&price=low&rating=4`

Instead of creating hundreds of different routes, query parameters allow flexible filtering using one route.

That is why they are heavily used in APIs.

* * *

### Key Difference Between Params and Query

This is where beginners usually get confused.

**URL Params**

Used for:

*   identifying a specific resource
    

Example:

`/users/101`

Meaning: get user with ID 101

* * *

### Query Parameters

Used for:

*   filtering
    
*   sorting
    
*   optional data
    
*   modifying output
    

Example:

`/products?category=shoes`

Meaning: Show products filtered by category

* * *

## Params vs Query (Simple Rule)

Think like this:

**Params → Required Identity**

Without it, the server does not know *which resource* you want.

Example:

/users/:id

* * *

### Query → Optional Modifiers

They change the result but are not always required.

Example:

`/products?sort=price`

The route still works even without sorting.

* * *

### Practical Example Combining Both

Example:

`/users/101/orders?status=completed`

Here:

*   `101` → URL parameter
    
*   `status=completed` → query parameter
    

Meaning:

Get completed orders for user 101

This is how real APIs are designed.

* * *

## Common Beginner Mistake

Many beginners think query parameters and URL parameters are interchangeable.

They are not.

Wrong thinking:

`/users?id=101`

This works technically, but if the ID is essential for identifying the resource, params are usually the better design.

Good API design matters.

* * *

### How to Think About It

Simple rule:

*   Params → “Which resource?”
    
*   Query → “How should the result change?”
    

Once this clicks, the confusion disappears.

* * *

### One Line Summary

URL parameters identify resources. Query parameters modify or filter results.

* * *

### Conclusion

URL parameters and query parameters both help send information through the URL, but they solve different problems.

Params are mainly used for identifying specific resources, while query parameters are used for filtering, searching, sorting, and modifying responses.

Understanding this difference is important because almost every backend API uses both concepts together.

Once you understand when to use each one, designing routes in Express becomes much cleaner and more logical.
