Skip to main content

URL Parameters vs Query Strings in Express.js

Updated 6 min readMarkdown

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:

/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:

/users/101

Here:

101

is a URL parameter.

It identifies:

Which user?

URL Structure Breakdown

/users/101
       ↑
    URL Parameter

Example: User Profile ID

Suppose we want to fetch a specific user profile.

Example route:

/users/25

Meaning:

Get user whose ID is 25

This is a perfect use case for URL parameters because:

The ID identifies a specific resource

Defining URL Params in Express

Express uses:

:paramName

syntax.

Example:

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

If the request is:

/users/42

Output:

42

Accessing Params in Express

Express stores route parameters inside:

req.params

Example:

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

Request:

/products/500

Result:

{
  productId: "500"
}

Multiple URL Parameters

Example:

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

URL:

/users/10/posts/7

Result:

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

What Are Query Parameters?

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

Example:

/products?category=shoes

Here:

category=shoes

is a query parameter.

Query parameters are usually used for:

  • filtering

  • sorting

  • pagination

  • searching

  • modifiers


URL Structure Breakdown for Query Params

/products?category=shoes
          ↑
      Query String

Example: Search Filters

Suppose we want:

Only shoes
Sorted by price
Page 2

The URL may look like:

/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:

req.query

Example:

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

Request:

/products?category=shoes&page=2

Result:

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

Multiple Query Parameters

Example:

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

Express receives:

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

Core Difference Between Params and Query

This is the most important section.


URL Parameters

Used for:

Identifying specific resources

Examples:

/users/10
/products/7
/posts/100

Meaning:

Give me THIS exact thing

Query Parameters

Used for:

Filtering or modifying results

Examples:

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

Meaning:

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

/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:

/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:

/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

/products/101

Meaning:

Open product with ID 101

Query Params Example

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

Meaning:

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

Very different purpose.


Common Beginner Mistake

Some beginners put everything into params:

/products/shoes/nike/page/2

Others put everything into query strings:

/products?id=101

Technically possible.

Architecturally ugly.

Good API design makes URLs meaningful and predictable.


Practical Mental Model

Params

WHO or WHAT?

Query

HOW or WHICH OPTIONS?

Another Simple Analogy

Imagine entering a library.

URL Param

/book/101

Means:

Give me THIS specific book

Query Param

/books?genre=scifi&sort=rating

Means:

Show books matching filters

Full Example in Express

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

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

Request:

/users/25?posts=true

Response:

{
  "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:

?page=2

becomes:

"2"

not:

2

So developers often convert values manually.

Example:

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

Final Mental Shortcut

Whenever designing routes, ask:

Is this identifying something specific?

Use:

URL Params

Is this modifying/filtering/searching?

Use:

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:

Params → resource identity
Query → response customization

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

1 view

No comments yet. Be the first to comment.