# URL Parameters vs Query Strings in Express.js

When building APIs or web applications with Express.js, you often need to send information through URLs.

For example:

*   Which user profile should be opened?
    
*   Which product should be displayed?
    
*   Which search filters should be applied?
    

This information is commonly passed using:

*   URL Parameters
    
*   Query Strings
    

Beginners often confuse these two concepts because both appear inside URLs.

But they solve different problems.

In this blog, you’ll learn:

*   What URL parameters are
    
*   What query parameters are
    
*   Their differences
    
*   How to access them in Express
    
*   When to use each approach
    

* * *

# Understanding URLs First

Before learning params and query strings, let’s understand a basic URL.

Example:

```text
https://example.com/users/101?sort=asc
```

This URL contains:

*   Path information
    
*   Query information
    

* * *

# URL Structure Breakdown

```text
https://example.com/users/101?sort=asc
                    │      │
                    │      └── Query String
                    │
                    └── URL Parameter
```

Both help send data to the server.

But they have different purposes.

* * *

# What Are URL Parameters?

URL parameters are values embedded directly inside the URL path.

They usually identify:

*   A specific resource
    
*   A specific item
    

* * *

# Real-Life Analogy

Think of URL params like:

*   A house number
    

Example:

```text
House #101
```

The number uniquely identifies a house.

Similarly:

```text
/users/101
```

identifies:

*   User 101
    

* * *

# Example URL Parameter

```text
/users/101
```

Here:

*   `101` is a URL parameter
    

It identifies:

*   Which user we want
    

* * *

# Express Route Example

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

* * *

# Understanding `:id`

The `:id` part means:

> “This value is dynamic.”

Express captures the value from the URL.

* * *

# Example Request

```text
/users/101
```

* * *

# Result

```javascript
req.params.id
// 101
```

* * *

# Accessing Params in Express

Express stores URL parameters inside:

```javascript
req.params
```

* * *

# Example

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

If the URL is:

```text
/products/55
```

then:

```javascript
req.params.productId
// 55
```

* * *

# What Are Query Parameters?

Query parameters are extra values added after a `?` in the URL.

They usually modify or filter data.

* * *

# Example Query String

```text
/products?category=phones
```

Here:

*   `category=phones` is a query parameter
    

* * *

# Real-Life Analogy

Think of query strings like:

*   Search filters in online shopping
    

Example:

*   Color = black
    
*   Price < 5000
    
*   Brand = Samsung
    

The main page stays the same:

*   `/products`
    

But filters change the results.

* * *

# Query Parameters Usually Represent

*   Filters
    
*   Sorting
    
*   Pagination
    
*   Search terms
    
*   Optional settings
    

* * *

# Accessing Query Strings in Express

Express stores query parameters inside:

```javascript
req.query
```

* * *

# Example

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

Request:

```text
/products?category=phones
```

Result:

```javascript
req.query.category
// phones
```

* * *

# Multiple Query Parameters

Example:

```text
/products?category=phones&sort=price
```

Now the query object becomes:

```javascript
{
  category: "phones",
  sort: "price"
}
```

* * *

# Params vs Query: Core Difference

This is the most important concept.

* * *

# URL Parameters

Usually identify:

> Which specific resource?

Example:

```text
/users/101
```

Meaning:

*   User with ID 101
    

* * *

# Query Parameters

Usually modify:

> How data should be returned?

Example:

```text
/users?sort=asc
```

Meaning:

*   Return users sorted ascending
    

* * *

# Params vs Query Comparison Diagram

```text
URL Params
/users/101
        ↑
   Specific Resource

Query Params
/users?sort=asc
        ↑
 Filter / Modifier
```

* * *

# Side-by-Side Comparison

| Feature | URL Params | Query Params |
| --- | --- | --- |
| Position | Inside URL path | After `?` |
| Purpose | Identify resource | Filter or modify |
| Express Access | `req.params` | `req.query` |
| Usually Required | Yes | Often optional |

* * *

# User Profile Example

## URL Parameter

```text
/users/101
```

Meaning:

*   Open profile of user 101
    

The ID is essential.

* * *

# Search Filter Example

## Query Parameter

```text
/search?keyword=laptop
```

Meaning:

*   Search for laptops
    

The keyword modifies results.

* * *

# Practical Express Example

## URL Params Example

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

Request:

```text
/users/42
```

Response:

```text
Viewing user 42
```

* * *

# Query Params Example

```javascript
app.get("/search", (req, res) => {
  res.send(`Searching for ${req.query.keyword}`);
});
```

Request:

```text
/search?keyword=javascript
```

Response:

```text
Searching for javascript
```

* * *

# When to Use URL Params

Use params when:

*   The value identifies something specific
    

Examples:

*   User ID
    
*   Product ID
    
*   Order ID
    
*   Blog post slug
    

* * *

# Good Examples

```text
/users/101
/products/55
/orders/999
```

* * *

# When to Use Query Params

Use query strings when:

*   Filtering
    
*   Searching
    
*   Sorting
    
*   Pagination
    
*   Optional settings
    

* * *

# Good Examples

```text
/products?category=phones
/search?keyword=nodejs
/users?page=2
```

* * *

# Common Beginner Confusion

## “Can IDs Be Passed in Query Strings?”

Yes.

Example:

```text
/users?id=101
```

This technically works.

But REST-style APIs usually prefer:

```text
/users/101
```

because:

*   It clearly identifies a specific resource
    

* * *

# Easy Rule to Remember

## URL Params

```text
WHO or WHAT?
```

* * *

# Query Params

```text
HOW?
```

* * *

# Full Example URL

```text
/products/55?color=black&sort=price
```

Breakdown:

| Part | Meaning |
| --- | --- |
| `/products/55` | Specific product |
| `color=black` | Filter |
| `sort=price` | Sort option |

* * *

# Complete URL Visualization

```text
/products/55?color=black&sort=price
          │
          └── URL Parameter

                    │
                    └── Query Parameters
```

* * *

# Why This Matters in Backend Development

You’ll use params and query strings constantly in:

*   REST APIs
    
*   Express apps
    
*   Search systems
    
*   E-commerce websites
    
*   Dashboards
    

Understanding the difference helps you design cleaner APIs.

* * *

# Practice Exercises

## Exercise 1

Create a route:

```text
/users/:id
```

Return the user ID.

* * *

# Exercise 2

Create a route:

```text
/search?keyword=phone
```

Return the search keyword.

* * *

# Exercise 3

Try combining both:

```text
/products/55?color=red
```

Understand:

*   Which part is param
    
*   Which part is query
    

* * *

# Final Thoughts

URL parameters and query strings both help send information through URLs, but they serve different purposes.

The easiest way to remember them:

| Type | Purpose |
| --- | --- |
| URL Params | Identify resource |
| Query Params | Filter or modify results |

In Express:

*   `req.params` handles route values
    
*   `req.query` handles query strings
    

Once this distinction becomes clear, working with APIs and backend routes becomes much easier.
