Skip to main content

Command Palette

Search for a command to run...

URL Parameters vs Query Strings in Express.js

Published
5 min read
URL Parameters vs Query Strings in Express.js

When building APIs or web applications with Express.js, you’ll frequently work with dynamic URLs. Sometimes you need to identify a specific resource like a user or product, and other times you need to filter or modify data such as searching, sorting, or pagination.

This is where URL parameters and query strings come into play.

At first, both may look similar because they pass data through the URL, but they serve completely different purposes. Understanding when to use each one is important for designing clean and professional APIs.

In this article, we’ll understand the difference between URL parameters and query strings in a simple and practical way using Express.js examples.


What URL Parameters Are ?

URL parameters, also called route parameters, are dynamic values written directly inside the URL path.

They are mainly used to identify a specific resource.

For example:

/users/25

Here:

  • users represents the route

  • 25 represents a specific user ID

In this case, 25 is a URL parameter.

Think of URL parameters as identifiers.

Examples:

  • user ID

  • product ID

  • blog slug

  • order ID

Example routes:


/products/101
/blog/react-hooks-guide
/orders/9001

Each URL points to one specific resource.

Accessing params in Express

In Express, route parameters are accessed using:

req.params

Example:


import express from "express";

const app = express();

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

  const userId = req.params.id;

  res.send(`User ID is ${userId}`);

});

app.listen(3000);

If the user visits:

http://localhost:3000/users/25

Response:

User ID is 25

The :id part is called a route parameter placeholder.

Express automatically extracts the value and stores it inside req.params.

You can also use multiple parameters:

app.get("/users/:userId/posts/:postId", (req, res) => {

  res.json(req.params);

});

URL:

/users/25/posts/100

Response:

{
  "userId": "25",
  "postId": "100"
}

What query parameters are

Query parameters, also called query strings, are extra values added after a ? in the URL.

They are usually used for:

  • filtering

  • sorting

  • searching

  • pagination

  • modifying results

Example:

/products?category=mobile

Here:

  • /products is the main route

  • category=mobile is a query parameter

Unlike route parameters, query strings do not identify a specific resource.

Instead, they modify the response.

Think of query parameters as filters or modifiers.

Accessing query strings in Express

In Express, query parameters are accessed using:

req.query

Example:


import express from "express";

const app = express();

app.get("/products", (req, res) => {

  const category = req.query.category;

  res.send(`Category is ${category}`);

});

app.listen(3000);

URL:

http://localhost:3000/products?category=mobile

Response:

Category is mobile

Multiple query parameters can also be used:

/products?category=mobile&sort=price&page=2

Express converts them into an object:

{
  category: "mobile",
  sort: "price",
  page: "2"
}

Differences between them

Although both are passed through the URL, their purpose is completely different.

Feature URL Parameters Query Parameters
Purpose Identify a specific resource Filter or modify data
Position Inside URL path After ?
Required Usually required Usually optional
Example /users/25 /users?age=25
Usage IDs, slugs search, filters, sorting

Understanding with practical examples

Example 1: User profile ID

/users/25

Here:

  • 25 identifies a specific user

  • URL parameter should be used

Express route:


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

  const id = req.params.id;

  res.send(`Fetching user ${id}`);

});

Example 2: Search filters

/products?category=mobile&brand=samsung

Here:

  • we are filtering products

  • query strings should be used

Express route:

app.get("/products", (req, res) => {

  const { category, brand } = req.query;

  res.json({
    category,
    brand
  });

});

URL structure breakdown

Example URL:

/users/25/orders?status=completed&page=1

Breakdown:

/users           → route
25               → URL parameter
/orders          → nested route
status=completed → query parameter
page=1           → query parameter

This clearly shows how both params and query strings can exist together in the same URL.


When To Use Params vs Query

A simple rule developers commonly follow:

Use URL parameters when:

  • identifying a specific resource

  • fetching one item

  • route structure depends on value

Examples:

  • /users/10

  • /products/55

  • /posts/react-guide

Use query strings when:

  • filtering data

  • sorting results

  • searching

  • pagination

  • optional modifiers

Examples:

  • /products?category=laptop

  • /users?page=2

  • /search?keyword=nodejs

Params vs query comparison diagram


URL Parameter Example:
--------------------------------
/users/25
       ↑
    Specific Resource ID


Query Parameter Example:
--------------------------------
/users?age=25&city=Delhi
      ↑
   Filters / Modifiers

Conclusion

URL parameters and query strings may look similar, but they solve different problems in web applications. Route parameters are mainly used for identifying specific resources, while query strings are used for filtering, searching, sorting, or modifying results.

Understanding this difference helps you design cleaner APIs and makes your routes easier to understand and maintain. A good Express application uses both properly depending on the purpose of the request.

Once you become comfortable with params and query strings, building dynamic routes and flexible APIs in Express.js becomes much more intuitive.