# URL Parameters vs Query Strings in Express.js

When building APIs in Express.js, we often need to send data through URLs.

For example:

*   Fetching a specific user profile
    
*   Searching products
    
*   Filtering results
    
*   Pagination
    

This is where:

*   URL Parameters
    
*   Query Strings
    

become very important.

In this article, we’ll learn:

*   What URL parameters are
    
*   What query parameters are
    
*   Differences between them
    
*   How to access them in Express
    
*   When to use params vs query strings
    

Let’s begin with the basics.

* * *

## Understanding URL Structure

Example URL:

```id="h2u0f7"
http://localhost:3000/users/101?active=true
```

This URL contains both:

*   URL parameter
    
*   Query parameter
    

Breakdown:

```id="n0qjlwm"
/users/101
        ↑
    URL Parameter

?active=true
       ↑
   Query Parameter
```

* * *

## What Are URL Parameters?

URL parameters are values placed directly inside the URL path.

They usually identify a specific resource.

Example:

```id="epjlwm"
/users/101
```

Here:

```id="3w4c8v"
101
```

is the parameter.

It commonly represents:

*   User ID
    
*   Product ID
    
*   Blog ID
    

Think of params as:

```id="4v8fqs"
Identifiers
```

* * *

## Real-World Example

Suppose we want details of a specific user.

URL:

```id="sjlwmx"
/users/101
```

Meaning:

```id="8lbmne"
"Fetch user whose ID is 101"
```

This uniquely identifies a resource.

* * *

## Defining URL Params in Express

Example:

```javascript
const express = require("express");

const app = express();

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

app.listen(3000);
```

* * *

## Accessing Params in Express

We use:

```javascript
req.params
```

Example request:

```id="ap53lh"
/users/101
```

Output:

```id="s8k3cw"
User ID is 101
```

Here:

```javascript
req.params.id
```

contains:

```id="jlwm9"
101
```

* * *

## Multiple URL Parameters

Example:

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

Request:

```id="6ec4j0"
/users/10/posts/55
```

Result:

```javascript
{
  userId: "10",
  postId: "55"
}
```

* * *

## What Are Query Parameters?

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

Example:

```id="78u5fa"
/products?category=mobile
```

Here:

```id="rjlwmz"
category=mobile
```

is a query parameter.

Query strings are usually used for:

*   Filters
    
*   Search
    
*   Sorting
    
*   Pagination
    

Think of query params as:

```id="fjlwm7"
Modifiers or Filters
```

* * *

## Real-World Example

Suppose we want to search products.

URL:

```id="d9mjlwm"
/products?category=shoes&price=2000
```

Meaning:

```id="1l93m8"
"Show shoes with price 2000"
```

The resource is still:

```id="7ccz4d"
/products
```

but the query changes how results are filtered.

* * *

## Accessing Query Strings in Express

We use:

```javascript
req.query
```

Example:

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

Request:

```id="7jlwm5"
/products?category=mobile&brand=apple
```

Result:

```javascript
{
  category: "mobile",
  brand: "apple"
}
```

* * *

## Query Parameter Breakdown

```id="2rfjlwm"
/products?category=mobile&brand=apple

/products
     ↑
 Resource

category=mobile
brand=apple
     ↑
 Query Parameters
```

* * *

## Params vs Query Strings

| Feature | URL Params | Query Strings |
| --- | --- | --- |
| Purpose | Identify resource | Filter or modify results |
| Position | Inside URL path | After `?` |
| Example | `/users/10` | `/users?active=true` |
| Accessed Using | `req.params` | `req.query` |
| Common Usage | IDs | Search, filters, sorting |

* * *

## Simple Comparison

### URL Parameter Example

```id="4qjlwm"
/users/25
```

Meaning:

```id="8jlwm4"
Get user with ID 25
```

* * *

### Query String Example

```id="jlwm18"
/users?country=india
```

Meaning:

```id="x0k1dp"
Get users filtered by country
```

* * *

## When Should You Use URL Params?

Use params when:

✅ Identifying a specific resource ✅ Resource is required ✅ URL represents a unique entity

Examples:

```id="mejlwm"
/users/5
/products/10
/posts/100
```

* * *

## When Should You Use Query Strings?

Use query strings when:

✅ Filtering data ✅ Searching ✅ Sorting ✅ Pagination

Examples:

```id="jlwm31"
/products?category=laptop

/users?page=2

/posts?sort=latest
```

* * *

## Real-World API Examples

### Fetch Specific Product

```id="3jlwmv"
/products/101
```

Uses URL params.

* * *

### Search Products

```id="jlwm67"
/products?category=phone&brand=samsung
```

Uses query strings.

* * *

## Combining Params and Query

Sometimes both are used together.

Example:

```id="rjlwm8"
/users/10/orders?status=completed
```

Here:

*   `10` → URL param
    
*   `status=completed` → query param
    

Meaning:

```id="jlwm90"
Get completed orders for user 10
```

* * *

## Practice Assignment

Try these exercises yourself.

* * *

## 1\. Create a Route with Params

Example:

```id="jlwm12"
/students/1
```

Return the student ID using:

```javascript
req.params
```

* * *

## 2\. Create a Route with Query Strings

Example:

```id="jlwm54"
/search?keyword=nodejs
```

Return the query value using:

```javascript
req.query
```

* * *

## 3\. Combine Both

Try:

```id="jlwm65"
/users/5/posts?sort=latest
```

Understand:

*   Which part is param
    
*   Which part is query
    

* * *

## Final Thoughts

URL parameters and query strings are fundamental concepts in Express.js APIs.

The key idea is simple:

*   Params identify resources
    
*   Query strings modify or filter resources
    

Understanding when to use each helps you design cleaner and more professional APIs.

As you continue building backend applications, you’ll use params and query strings almost everywhere:

*   User systems
    
*   Product APIs
    
*   Search functionality
    
*   Pagination
    
*   Filters
    

Mastering these concepts is an important step toward becoming a strong backend developer.

* * *

And now, you know what URL Parameters and Query Strings in Express.js are.

If you have any doubt or want to connect, feel free to drop a comment — I’d be happy to help.

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!
