Skip to main content

Command Palette

Search for a command to run...

URL Parameters vs Query Strings in Express.js

Updated
6 min readView as Markdown

When building APIs or web applications with Express.js, you often need to send information through URLs.

For example:

  • Which user profile should be opened?

  • Which product should be displayed?

  • Which search filters should be applied?

This information is commonly passed using:

  • URL Parameters

  • Query Strings

Beginners often confuse these two concepts because both appear inside URLs.

But they solve different problems.

In this blog, you’ll learn:

  • What URL parameters are

  • What query parameters are

  • Their differences

  • How to access them in Express

  • When to use each approach


Understanding URLs First

Before learning params and query strings, let’s understand a basic URL.

Example:

https://example.com/users/101?sort=asc

This URL contains:

  • Path information

  • Query information


URL Structure Breakdown

https://example.com/users/101?sort=asc
                    │      │
                    │      └── Query String
                    │
                    └── URL Parameter

Both help send data to the server.

But they have different purposes.


What Are URL Parameters?

URL parameters are values embedded directly inside the URL path.

They usually identify:

  • A specific resource

  • A specific item


Real-Life Analogy

Think of URL params like:

  • A house number

Example:

House #101

The number uniquely identifies a house.

Similarly:

/users/101

identifies:

  • User 101

Example URL Parameter

/users/101

Here:

  • 101 is a URL parameter

It identifies:

  • Which user we want

Express Route Example

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

Understanding :id

The :id part means:

“This value is dynamic.”

Express captures the value from the URL.


Example Request

/users/101

Result

req.params.id
// 101

Accessing Params in Express

Express stores URL parameters inside:

req.params

Example

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

If the URL is:

/products/55

then:

req.params.productId
// 55

What Are Query Parameters?

Query parameters are extra values added after a ? in the URL.

They usually modify or filter data.


Example Query String

/products?category=phones

Here:

  • category=phones is a query parameter

Real-Life Analogy

Think of query strings like:

  • Search filters in online shopping

Example:

  • Color = black

  • Price < 5000

  • Brand = Samsung

The main page stays the same:

  • /products

But filters change the results.


Query Parameters Usually Represent

  • Filters

  • Sorting

  • Pagination

  • Search terms

  • Optional settings


Accessing Query Strings in Express

Express stores query parameters inside:

req.query

Example

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

Request:

/products?category=phones

Result:

req.query.category
// phones

Multiple Query Parameters

Example:

/products?category=phones&sort=price

Now the query object becomes:

{
  category: "phones",
  sort: "price"
}

Params vs Query: Core Difference

This is the most important concept.


URL Parameters

Usually identify:

Which specific resource?

Example:

/users/101

Meaning:

  • User with ID 101

Query Parameters

Usually modify:

How data should be returned?

Example:

/users?sort=asc

Meaning:

  • Return users sorted ascending

Params vs Query Comparison Diagram

URL Params
/users/101
        ↑
   Specific Resource

Query Params
/users?sort=asc
        ↑
 Filter / Modifier

Side-by-Side Comparison

Feature URL Params Query Params
Position Inside URL path After ?
Purpose Identify resource Filter or modify
Express Access req.params req.query
Usually Required Yes Often optional

User Profile Example

URL Parameter

/users/101

Meaning:

  • Open profile of user 101

The ID is essential.


Search Filter Example

Query Parameter

/search?keyword=laptop

Meaning:

  • Search for laptops

The keyword modifies results.


Practical Express Example

URL Params Example

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

Request:

/users/42

Response:

Viewing user 42

Query Params Example

app.get("/search", (req, res) => {
  res.send(`Searching for ${req.query.keyword}`);
});

Request:

/search?keyword=javascript

Response:

Searching for javascript

When to Use URL Params

Use params when:

  • The value identifies something specific

Examples:

  • User ID

  • Product ID

  • Order ID

  • Blog post slug


Good Examples

/users/101
/products/55
/orders/999

When to Use Query Params

Use query strings when:

  • Filtering

  • Searching

  • Sorting

  • Pagination

  • Optional settings


Good Examples

/products?category=phones
/search?keyword=nodejs
/users?page=2

Common Beginner Confusion

“Can IDs Be Passed in Query Strings?”

Yes.

Example:

/users?id=101

This technically works.

But REST-style APIs usually prefer:

/users/101

because:

  • It clearly identifies a specific resource

Easy Rule to Remember

URL Params

WHO or WHAT?

Query Params

HOW?

Full Example URL

/products/55?color=black&sort=price

Breakdown:

Part Meaning
/products/55 Specific product
color=black Filter
sort=price Sort option

Complete URL Visualization

/products/55?color=black&sort=price
          │
          └── URL Parameter

                    │
                    └── Query Parameters

Why This Matters in Backend Development

You’ll use params and query strings constantly in:

  • REST APIs

  • Express apps

  • Search systems

  • E-commerce websites

  • Dashboards

Understanding the difference helps you design cleaner APIs.


Practice Exercises

Exercise 1

Create a route:

/users/:id

Return the user ID.


Exercise 2

Create a route:

/search?keyword=phone

Return the search keyword.


Exercise 3

Try combining both:

/products/55?color=red

Understand:

  • Which part is param

  • Which part is query


Final Thoughts

URL parameters and query strings both help send information through URLs, but they serve different purposes.

The easiest way to remember them:

Type Purpose
URL Params Identify resource
Query Params Filter or modify results

In Express:

  • req.params handles route values

  • req.query handles query strings

Once this distinction becomes clear, working with APIs and backend routes becomes much easier.