# URL Parameters vs Query Strings in Express.js

When working with APIs and backend development, one of the first confusing topics beginners encounter is:

*   URL parameters
    
*   Query parameters
    

At first glance they look similar.

Both send data through the URL.

Both are used in routes.

Both somehow appear inside Express.

Then suddenly beginners start throwing everything into query strings like:

```text
/api/user?id=7&type=admin&page=2&edit=true
```

while others create routes that look like encrypted subway maps.

The real difference is actually simple.

> URL params identify resources. Query params modify or filter data.

Once you understand that distinction, routing becomes much more logical.

* * *

# What Are URL Parameters?

URL parameters are dynamic values inside the route path.

They are mainly used to identify a specific resource.

Example:

```text
/users/101
```

Here:

```text
101
```

is a URL parameter.

It identifies:

```text
Which user?
```

* * *

# URL Structure Breakdown

```text
/users/101
       ↑
    URL Parameter
```

* * *

# Example: User Profile ID

Suppose we want to fetch a specific user profile.

Example route:

```text
/users/25
```

Meaning:

```text
Get user whose ID is 25
```

This is a perfect use case for URL parameters because:

```text
The ID identifies a specific resource
```

* * *

# Defining URL Params in Express

Express uses:

```js
:paramName
```

syntax.

Example:

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

If the request is:

```text
/users/42
```

Output:

```text
42
```

* * *

# Accessing Params in Express

Express stores route parameters inside:

```js
req.params
```

Example:

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

Request:

```text
/products/500
```

Result:

```js
{
  productId: "500"
}
```

* * *

# Multiple URL Parameters

Example:

```js
app.get("/users/:userId/posts/:postId", (req, res) => {
  console.log(req.params);
});
```

URL:

```text
/users/10/posts/7
```

Result:

```js
{
  userId: "10",
  postId: "7"
}
```

* * *

# What Are Query Parameters?

Query parameters are key-value pairs added after a question mark (`?`) in the URL.

Example:

```text
/products?category=shoes
```

Here:

```text
category=shoes
```

is a query parameter.

Query parameters are usually used for:

*   filtering
    
*   sorting
    
*   pagination
    
*   searching
    
*   modifiers
    

* * *

# URL Structure Breakdown for Query Params

```text
/products?category=shoes
          ↑
      Query String
```

* * *

# Example: Search Filters

Suppose we want:

```text
Only shoes
Sorted by price
Page 2
```

The URL may look like:

```text
/products?category=shoes&sort=price&page=2
```

This is not identifying one product.

It is modifying how data should be returned.

That is why query parameters are used.

* * *

# Accessing Query Parameters in Express

Express stores query strings inside:

```js
req.query
```

Example:

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

Request:

```text
/products?category=shoes&page=2
```

Result:

```js
{
  category: "shoes",
  page: "2"
}
```

* * *

# Multiple Query Parameters

Example:

```text
/search?keyword=react&sort=latest&page=3
```

Express receives:

```js
{
  keyword: "react",
  sort: "latest",
  page: "3"
}
```

* * *

# Core Difference Between Params and Query

This is the most important section.

* * *

# URL Parameters

Used for:

```text
Identifying specific resources
```

Examples:

```text
/users/10
/products/7
/posts/100
```

Meaning:

```text
Give me THIS exact thing
```

* * *

# Query Parameters

Used for:

```text
Filtering or modifying results
```

Examples:

```text
/products?category=shoes
/search?keyword=react
/posts?page=2
```

Meaning:

```text
Customize the response
```

* * *

# Params vs Query Comparison Table

| Feature | URL Params | Query Params |
| --- | --- | --- |
| Position | Inside route path | After `?` |
| Purpose | Identify resource | Modify/filter data |
| Express access | `req.params` | `req.query` |
| Required usually? | Often yes | Usually optional |
| Example | `/users/10` | `/users?page=2` |
| Best for | Specific item | Search/filter/sort |

* * *

# Diagram: Params vs Query

```text
/users/15?tab=posts
       ↑       ↑
    Params   Query
```

* * *

# When to Use URL Params

Use params when:

*   accessing a specific resource
    
*   resource identity matters
    
*   route requires an identifier
    

Examples:

```text
/users/10
/products/45
/orders/900
```

These routes point to exact entities.

* * *

# When to Use Query Params

Use query parameters when:

*   filtering results
    
*   searching
    
*   sorting
    
*   pagination
    
*   optional modifiers
    

Examples:

```text
/products?category=electronics
/users?sort=latest
/posts?page=3
/search?keyword=nodejs
```

These modify how data is returned.

* * *

# Real-World Example

Imagine an e-commerce website.

## URL Params Example

```text
/products/101
```

Meaning:

```text
Open product with ID 101
```

* * *

## Query Params Example

```text
/products?category=shoes&brand=nike&page=2
```

Meaning:

```text
Show products filtered by:
- category
- brand
- page number
```

Very different purpose.

* * *

# Common Beginner Mistake

Some beginners put everything into params:

```text
/products/shoes/nike/page/2
```

Others put everything into query strings:

```text
/products?id=101
```

Technically possible.

Architecturally ugly.

Good API design makes URLs meaningful and predictable.

* * *

# Practical Mental Model

## Params

```text
WHO or WHAT?
```

* * *

## Query

```text
HOW or WHICH OPTIONS?
```

* * *

# Another Simple Analogy

Imagine entering a library.

## URL Param

```text
/book/101
```

Means:

```text
Give me THIS specific book
```

* * *

## Query Param

```text
/books?genre=scifi&sort=rating
```

Means:

```text
Show books matching filters
```

* * *

# Full Example in Express

```js
app.get("/users/:id", (req, res) => {
  const userId = req.params.id;
  const showPosts = req.query.posts;

  res.send({
    userId,
    showPosts
  });
});
```

Request:

```text
/users/25?posts=true
```

Response:

```json
{
  "userId": "25",
  "showPosts": "true"
}
```

Here:

*   `25` identifies the user
    
*   `posts=true` modifies the response
    

Perfect separation of responsibilities.

* * *

# Important Note About Types

Everything from URLs arrives as strings.

Example:

```text
?page=2
```

becomes:

```js
"2"
```

not:

```js
2
```

So developers often convert values manually.

Example:

```js
const page = Number(req.query.page);
```

* * *

# Final Mental Shortcut

Whenever designing routes, ask:

## Is this identifying something specific?

Use:

```text
URL Params
```

* * *

## Is this modifying/filtering/searching?

Use:

```text
Query Params
```

* * *

# Conclusion

URL parameters and query parameters both send information through the URL, but they serve different purposes.

*   URL params identify specific resources
    
*   Query params modify or filter responses
    

In Express:

*   URL params are accessed using `req.params`
    
*   Query params are accessed using `req.query`
    

The biggest conceptual difference is:

```text
Params → resource identity
Query → response customization
```

Understanding this distinction helps create cleaner APIs, more readable routes, and better backend architecture.
