# URL Parameters vs Query Strings in Express.js

In this article we'll learn about the URL params and query strings in express.js. For a beginner it hard not to have the doubt between this topic. We'll be covering the following in sequences:

1.  What URL parameters are
    
2.  What query parameters are
    
3.  Differences between them
    
4.  Accessing params in Express
    
5.  Accessing query strings in Express
    
6.  When to use params vs query
    

What does we understand by the query string and URL parameters ? When you start working with backend using Express.js, one thing you’ll see very often is URL parameters and query strings. At first, both look similar, but they are used for different purposes.

Let’s understand them in a simple way

## 1\. What are URL Parameters?

URL parameters are part of the URL path itself. They are used to identify a specific resource.

### Example:

```javascript
/users/101
```

Here, `101` is a URL parameter.

In Express, we define it using `:`

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

If you open:

```javascript
/users/101
```

You’ll get:

```javascript
User ID is 101
```

*   Used to identify specific data
    
*   Mandatory (usually required in route)
    

* * *

## 2\. What are Query Parameters?

Query parameters come after the `?` in a URL and are used to send extra information.

### Example:

```javascript
/products?category=shoes&price=1000
```

Here:

*   `category=shoes`
    
*   `price=1000`
    

In Express:

```javascript
app.get('/products', (req, res) => {
  const category = req.query.category;
  const price = req.query.price;

  res.send(`Category: ${category}, Price: ${price}`);
});
```

If you open:

```javascript
/products?category=shoes&price=1000
```

You’ll get:

```javascript
Category: shoes, Price: 1000
```

*   Used for filtering, sorting, optional data
    

*   Optional (can be missing)
    

* * *

## 3\. Differences Between URL Params and Query Strings

| Feature | URL Parameters | Query Strings |
| --- | --- | --- |
| Position | Inside URL path | After `?` |
| Example | `/users/101` | `/users?id=101` |
| Purpose | Identify resource | Filter / extra info |
| Required | Usually yes | Optional |
| Access in Express | `req.params` | `req.query` |

* * *

## 4\. Accessing URL Params in Express

```javascript
app.get('/posts/:postId', (req, res) => {
  console.log(req.params);
  res.send(req.params.postId);
});
```

If URL is:

```plaintext
/posts/55
```

Output:

```plaintext
55
```

* * *

## 5\. Accessing Query Strings in Express

```typescript
app.get('/search', (req, res) => {
  console.log(req.query);
  res.send(req.query.keyword);
});
```

If URL is:

```javascript
/search?keyword=react
```

Output:

```javascript
react
```

* * *

## 6\. When to Use Params vs Query?

This is where most beginners get confused, so remember this simple rule

### Use URL Params when:

*   You are fetching specific resource
    
*   Example:
    
    *   `/users/101`
        
    *   `/posts/45`
        

### Use Query Strings when:

*   You are filtering or customizing results
    
*   Example:
    
    *   `/products?category=electronics`
        
    *   `/users?page=2&limit=10`
        

* * *

## Final Understanding

*   Params = "Which exact item?"
    
*   Query = "How do you want the data?"
    

* * *

## Bonus Tip

Both can be used together:

```javascript
/users/101?active=true
```

*   `101` → specific user (param)
    
*   `active=true` → extra condition (query)
    

* * *

That’s it! Once you understand this difference, working with APIs in Express becomes much easier!
