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
S

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.

In this article we'll learn about the URL params and query strings in express.js. For a beginner it hard not to have the doubt between this topic. We'll be covering the following in sequences:

  1. What URL parameters are

  2. What query parameters are

  3. Differences between them

  4. Accessing params in Express

  5. Accessing query strings in Express

  6. When to use params vs query

What does we understand by the query string and URL parameters ? When you start working with backend using Express.js, one thing you’ll see very often is URL parameters and query strings. At first, both look similar, but they are used for different purposes.

Let’s understand them in a simple way

1. What are URL Parameters?

URL parameters are part of the URL path itself. They are used to identify a specific resource.

Example:

/users/101

Here, 101 is a URL parameter.

In Express, we define it using :

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

If you open:

/users/101

You’ll get:

User ID is 101
  • Used to identify specific data

  • Mandatory (usually required in route)


2. What are Query Parameters?

Query parameters come after the ? in a URL and are used to send extra information.

Example:

/products?category=shoes&price=1000

Here:

  • category=shoes

  • price=1000

In Express:

app.get('/products', (req, res) => {
  const category = req.query.category;
  const price = req.query.price;

  res.send(`Category: \({category}, Price: \){price}`);
});

If you open:

/products?category=shoes&price=1000

You’ll get:

Category: shoes, Price: 1000
  • Used for filtering, sorting, optional data

  • Optional (can be missing)


3. Differences Between URL Params and Query Strings

Feature URL Parameters Query Strings
Position Inside URL path After ?
Example /users/101 /users?id=101
Purpose Identify resource Filter / extra info
Required Usually yes Optional
Access in Express req.params req.query

4. Accessing URL Params in Express

app.get('/posts/:postId', (req, res) => {
  console.log(req.params);
  res.send(req.params.postId);
});

If URL is:

/posts/55

Output:

55

5. Accessing Query Strings in Express

app.get('/search', (req, res) => {
  console.log(req.query);
  res.send(req.query.keyword);
});

If URL is:

/search?keyword=react

Output:

react

6. When to Use Params vs Query?

This is where most beginners get confused, so remember this simple rule

Use URL Params when:

  • You are fetching specific resource

  • Example:

    • /users/101

    • /posts/45

Use Query Strings when:

  • You are filtering or customizing results

  • Example:

    • /products?category=electronics

    • /users?page=2&limit=10


Final Understanding

  • Params = "Which exact item?"

  • Query = "How do you want the data?"


Bonus Tip

Both can be used together:

/users/101?active=true
  • 101 → specific user (param)

  • active=true → extra condition (query)


That’s it! Once you understand this difference, working with APIs in Express becomes much easier!