# URL Parameters vs Query Strings in Express.js 

When we build APIs or web applications using Node.js and Express, URLs become an important part of how data moves between the client and the server. Two common concepts you will see in almost every backend project are **URL parameters** and **query strings**.

At first, both may look similar because they both send values through the URL. But in real-world development, they are used for different purposes. Understanding when to use each one makes your API cleaner and easier to understand.

Your reference explained this idea using a library example.  
In this article, we will understand these concepts in a more practical way using different examples.

### Understanding URL Structure

Take a look at this URL:

```plaintext
http://localhost:3000/products/25/reviews?page=2&sort=latest
```

This URL contains both route parameters and query strings.

Breakdown:

```plaintext
/products/25/reviews
```

This part is the route path.

```plaintext
25
```

This is a URL parameter because it identifies a specific product.

Now look at this part:

```plaintext
?page=2&sort=latest
```

These are query strings because they change how the data is returned.

The `?` The symbol separates the main route from the query values.

### What are URL Parameters?

URL parameters are values written directly inside the route path. They are mainly used to identify a specific resource.

For example:

```plaintext
/products/25
```

Here, `25` could represent the ID of a product.

Another example:

```plaintext
/students/102
```

This route refers to a particular student.

In Express.js, parameters are created using a colon `:`.

Example:

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

app.get("/students/:studentId", (req, res) => {
  res.send(req.params);
});

app.listen(3000);
```

If the user visits:

```plaintext
/students/102
```

The output will be:

```json
{
  studentId: "102"
}
```

Express stores all route parameters inside:

```plaintext
req.params
```

### Multiple URL Parameters

You can also use more than one parameter in the same route.

Example:

```javascript
app.get("/courses/:courseId/lessons/:lessonId", (req, res) => {
  res.send(req.params);
});
```

Request:

```plaintext
/courses/12/lessons/5
```

Output:

```javascript
{
  courseId: "12",
  lessonId: "5"
}
```

This is useful when one resource belongs to another resource.

### What are Query Strings?

Query strings are optional values added after the `?` symbol in a URL. They are mostly used for filtering, searching, sorting, or pagination.

Example:

```plaintext
/products?category=mobile
```

Here, we are requesting products, but only from the mobile category.

Another example:

```plaintext
/movies?year=2025&rating=8
```

This route asks for movies filtered by year and rating.

In Express.js, query strings are available inside:

```plaintext
req.query
```

Example:

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

Request:

```plaintext
/movies?year=2025&rating=8
```

Output:

```plaintext
{
  year: "2025",
  rating: "8"
}
```

### Important Point About Query Values

Everything received from `req.query` is treated as a string.

Example:

```javascript
app.get("/items", (req, res) => {
  const page = parseInt(req.query.page) || 1;

  res.send(`Current page: ${page}`);
});
```

Request:

```plaintext
/items?page=3
```

Output:

```plaintext
Current page: 3
```

Without `parseInt()`, the value would remain a string.

### Difference Between Params and Query Strings

| URL Parameters | Query Strings |
| --- | --- |
| Part of the route | Added after `?` |
| Used for identification | Used for filtering or modifying |
| Usually required | Usually optional |
| Accessed with `req.params` | Accessed with `req.query` |
| Helps find a specific resource | Helps customize results |

### When Should You Use URL Parameters?

Use URL parameters when the value is necessary to identify something specific.

Example:

```plaintext
/orders/500
```

Without the order ID, the request does not make sense.

Express example:

```javascript
app.get("/orders/:orderId", (req, res) => {
  res.send(`Order ID: ${req.params.orderId}`);
});
```

### When Should You Use Query Strings?

Use query strings when the route can still work without the value.

Example:

```plaintext
/products?sort=price
```

Even without sorting, `/products` still works.

Another example:

```plaintext
/articles?tag=nodejs
```

This only filters articles by tag.

Express example:

```javascript
app.get("/articles", (req, res) => {
  const tag = req.query.tag;

  res.send(`Filtering by tag: ${tag}`);
});
```

### Combining Both Together

In real projects, both are often used in the same route.

Example:

```javascript
app.get("/teachers/:teacherId/classes", (req, res) => {
  const teacherId = req.params.teacherId;
  const subject = req.query.subject;

  res.send({
    teacherId,
    subject
  });
});
```

Request:

```plaintext
/teachers/45/classes?subject=math
```

Output:

```plaintext
{
  teacherId: "45",
  subject: "math"
}
```

Here:

*   `teacherId` identifies the teacher
    
*   `subject` filters the classes
    

### Common Mistakes

Many beginners place filters inside the route path.

Wrong:

```plaintext
/products/electronics
```

This looks like `electronics` is a product ID or product name.

Better:

```plaintext
/products?category=electronics
```

Another example:

Wrong:

```plaintext
/news/latest
```

Better:

```plaintext
/news?sort=latest
```

### Final Thoughts

URL parameters and query strings are both important parts of Express.js routing. Even though they look similar, their purpose is different.

*   URL parameters help identify a specific resource.
    
*   Query strings help modify or filter data.
    

A simple way to remember:

*   **Params → Which item?**
    
*   **Query → How should the result look?**
