Skip to main content

Command Palette

Search for a command to run...

URL Parameters vs Query Strings in Express.js

Published
4 min read

When building APIs with Express.js, understanding how data flows through URLs is fundamental. Two of the most common ways to pass data in a request URL are URL parameters (route params) and query strings.

They may look similar at first glance, but they serve very different purposes in API design.


🔹 What Are URL Parameters?

URL parameters (also called route parameters) are dynamic values embedded directly in the URL path.

They are typically used to identify a specific resource.

Example:

/users/123

Here:

  • 123 is a URL parameter

  • It represents a specific user ID

Express Example:

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

Key Characteristics:

  • Part of the URL path

  • Used as identifiers

  • Required to match the route

  • Clean and RESTful


🔹 What Are Query Parameters?

Query parameters are key-value pairs appended to the end of a URL after a ?.

They are used to modify or filter data, not identify it.

Example:

/users?age=25&city=Kolkata

Here:

  • age=25 and city=Kolkata are query parameters

  • They act as filters

Express Example:

app.get('/users', (req, res) => {
  const age = req.query.age;
  const city = req.query.city;

  res.send(`Filtering users by age \({age} and city \){city}`);
});

Key Characteristics:

  • Appear after ? in the URL

  • Used as filters, sorting, or modifiers

  • Optional by nature

  • Can have multiple values

Mind map -


🔹 Core Difference: Params vs Query

Feature URL Parameters Query Parameters
Purpose Identify a resource Filter or modify results
Location Inside URL path After ?
Example /users/123 /users?age=25
Express Access req.params req.query
Nature Required Optional

Mind map -

🔹 Practical Examples

1. User Profile (Identifier → Params)

GET /users/123
app.get('/users/:id', (req, res) => {
  const id = req.params.id;
  res.send(`Fetching user with ID ${id}`);
});

✔ Use params because you're targeting one specific user


2. Search or Filtering (Modifier → Query)

GET /users?age=25&city=Kolkata
app.get('/users', (req, res) => {
  const { age, city } = req.query;
  res.send(`Filtering users with age \({age} in \){city}`);
});

✔ Use query because you're refining results


3. Combining Both

You can use both together:

GET /users/123/posts?sort=latest
app.get('/users/:id/posts', (req, res) => {
  const userId = req.params.id;
  const sort = req.query.sort;

  res.send(`Posts of user \({userId}, sorted by \){sort}`);
});

🔹 When to Use Params vs Query

Use URL Parameters when:

  • You are accessing a specific resource

  • The value is mandatory

  • It represents an identity

✔ Examples:

  • /products/456

  • /orders/789


Use Query Parameters when:

  • You are filtering, sorting, or paginating

  • The values are optional

  • You want flexible API behavior

✔ Examples:

  • /products?category=electronics

  • /orders?status=delivered&page=2


🔹 Best Practices

  • Treat params as identifiers, not filters

  • Keep URLs clean and meaningful

  • Avoid mixing responsibilities (don’t use query for IDs)

  • Use query params for:

    • Pagination (page=1)

    • Sorting (sort=price)

    • Filtering (category=books)


🔹 Mental Model (Important)

  • Params = “Which exact thing?”

  • Query = “How should I view or filter it?”


🔹 Final Takeaway

If you're designing APIs:

  • Think of URL params as primary keys

  • Think of query strings as SQL WHERE clauses

This distinction leads to cleaner, more scalable, and RESTful API design.


If you want, I can next help you turn this into a perfect Hashnode blog with visuals + diagrams + SEO optimization, or even align it with your cohort grading rubric.

1 views