Skip to main content

Command Palette

Search for a command to run...

URL Parameters vs Query Strings in Express.js

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

When building APIs in Express.js, we often need to send data through URLs.

For example:

  • Fetching a specific user profile

  • Searching products

  • Filtering results

  • Pagination

This is where:

  • URL Parameters

  • Query Strings

become very important.

In this article, we’ll learn:

  • What URL parameters are

  • What query parameters are

  • Differences between them

  • How to access them in Express

  • When to use params vs query strings

Let’s begin with the basics.


Understanding URL Structure

Example URL:

http://localhost:3000/users/101?active=true

This URL contains both:

  • URL parameter

  • Query parameter

Breakdown:

/users/101
        ↑
    URL Parameter

?active=true
       ↑
   Query Parameter

What Are URL Parameters?

URL parameters are values placed directly inside the URL path.

They usually identify a specific resource.

Example:

/users/101

Here:

101

is the parameter.

It commonly represents:

  • User ID

  • Product ID

  • Blog ID

Think of params as:

Identifiers

Real-World Example

Suppose we want details of a specific user.

URL:

/users/101

Meaning:

"Fetch user whose ID is 101"

This uniquely identifies a resource.


Defining URL Params in Express

Example:

const express = require("express");

const app = express();

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

app.listen(3000);

Accessing Params in Express

We use:

req.params

Example request:

/users/101

Output:

User ID is 101

Here:

req.params.id

contains:

101

Multiple URL Parameters

Example:

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

Request:

/users/10/posts/55

Result:

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

What Are Query Parameters?

Query parameters are extra information added after ? in the URL.

Example:

/products?category=mobile

Here:

category=mobile

is a query parameter.

Query strings are usually used for:

  • Filters

  • Search

  • Sorting

  • Pagination

Think of query params as:

Modifiers or Filters

Real-World Example

Suppose we want to search products.

URL:

/products?category=shoes&price=2000

Meaning:

"Show shoes with price 2000"

The resource is still:

/products

but the query changes how results are filtered.


Accessing Query Strings in Express

We use:

req.query

Example:

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

Request:

/products?category=mobile&brand=apple

Result:

{
  category: "mobile",
  brand: "apple"
}

Query Parameter Breakdown

/products?category=mobile&brand=apple

/products
     ↑
 Resource

category=mobile
brand=apple
     ↑
 Query Parameters

Params vs Query Strings

Feature URL Params Query Strings
Purpose Identify resource Filter or modify results
Position Inside URL path After ?
Example /users/10 /users?active=true
Accessed Using req.params req.query
Common Usage IDs Search, filters, sorting

Simple Comparison

URL Parameter Example

/users/25

Meaning:

Get user with ID 25

Query String Example

/users?country=india

Meaning:

Get users filtered by country

When Should You Use URL Params?

Use params when:

✅ Identifying a specific resource ✅ Resource is required ✅ URL represents a unique entity

Examples:

/users/5
/products/10
/posts/100

When Should You Use Query Strings?

Use query strings when:

✅ Filtering data ✅ Searching ✅ Sorting ✅ Pagination

Examples:

/products?category=laptop

/users?page=2

/posts?sort=latest

Real-World API Examples

Fetch Specific Product

/products/101

Uses URL params.


Search Products

/products?category=phone&brand=samsung

Uses query strings.


Combining Params and Query

Sometimes both are used together.

Example:

/users/10/orders?status=completed

Here:

  • 10 → URL param

  • status=completed → query param

Meaning:

Get completed orders for user 10

Practice Assignment

Try these exercises yourself.


1. Create a Route with Params

Example:

/students/1

Return the student ID using:

req.params

2. Create a Route with Query Strings

Example:

/search?keyword=nodejs

Return the query value using:

req.query

3. Combine Both

Try:

/users/5/posts?sort=latest

Understand:

  • Which part is param

  • Which part is query


Final Thoughts

URL parameters and query strings are fundamental concepts in Express.js APIs.

The key idea is simple:

  • Params identify resources

  • Query strings modify or filter resources

Understanding when to use each helps you design cleaner and more professional APIs.

As you continue building backend applications, you’ll use params and query strings almost everywhere:

  • User systems

  • Product APIs

  • Search functionality

  • Pagination

  • Filters

Mastering these concepts is an important step toward becoming a strong backend developer.


And now, you know what URL Parameters and Query Strings in Express.js are.

If you have any doubt or want to connect, feel free to drop a comment — I’d be happy to help.

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!