Skip to main content

Command Palette

Search for a command to run...

URL Parameters vs Query Strings in Express.js

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

When you building any web applications with Express.js, you will often need to pass data through URLs. There are two common ways to do this, URL Parameters and Query Parameters.

Although both are used to send information in a URL, they serve different purposes. Let's talk about what does do both

What Are URL Parameters?

URL parameters are values that are part of the URL path. They are usually used to identify a specific resource.

Example:

/users/101

In this URL:

  • users is the route.

  • 101 is the URL parameter.

Here, 101 represents the ID of a specific user.

Practical Example

If you want to view the profile of a user:

/users/101

The user ID (101) identifies exactly which user's profile should be displayed.

Defining URL Parameters in Express

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

Accessing URL Parameters

Express stores URL parameters inside the req.params object.

const userId = req.params.id;
console.log(userId);

//output: 101

What Are Query Parameters?

Query parameters are added after a question mark (?) in a URL. They are commonly used for filtering, searching, sorting, or modifying data.

Example:

/products?category=mobile&brand=samsung

In this URL:

  • category=mobile

  • brand=samsung

are query parameters.

Practical Example

Suppose you have an online store and want to filter products.

/products?category=laptop&price=50000

This URL is requesting laptops with a specific price filter.

Accessing Query Parameters in Express

Express stores query parameters inside the req.query object.

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

You can access values like this:

const category = req.query.category;
const price = req.query.price;

Output:

{
  category: "laptop",
  price: "50000"
}

Key Differences Between URL Parameters and Query Parameters

URL Parameters Query Parameters
Used to identify a specific resource Used to filter or modify results
Part of the URL path Added after ? in the URL
Accessed using req.params Accessed using req.query
Usually required Usually optional
Example: /users/101 Example: /products?category=mobile

When Should You Use URL Parameters?

We use URL parameters when weneed to identify a specific resource.

Examples:

/users/101
/posts/25
/orders/500

These URLs parameter is pointing to single specific item.

When Should You Use Query Parameters?

Use query parameters when you want to filter, search, sort, or customize results.

Examples:

/products?category=laptop
/products?brand=apple
/products?sort=price
/products?page=2

These URLs change how data is displayed without identifying a specific resource.

URL Structure Breakdown

Example:

/users/101?showPosts=true

Breakdown:

/users          → Route
101             → URL Parameter
?               → Starts Query String
showPosts=true  → Query Parameter

Conclusion

URL parameters and query parameters are essential parts of web development in Express.js. URL parameters are mainly used to identify specific resources, while query parameters are used to filter or modify results. Express makes accessing both very simple through req.params and req.query. By choosing the right approach, you can create cleaner APIs and improve the overall user experience.