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

I used to mix these up constantly.

URL parameters.

Query strings.

People would say:

Use params here.

Use query there.

And I’d be thinking…

Aren’t both just values coming through the URL?

Why do we even have two ways?

That confused me for longer than I want to admit.

Then someone gave me one simple distinction.

And after that…

it became much easier.


The Simple Difference That Helped Me

URL parameters usually identify what you want.

Query strings usually modify how you want it.

That one sentence cleared up a lot.

Honestly…

I still think of it that way.


What URL Parameters Are

Params are values inside the route path itself.

Example:

/users/42

That:

42

can be a parameter.

Maybe it means:

User ID.

Specific user.

One resource.

That is the key idea.

Params often identify something specific.


I Think Of Params Like Addresses

This analogy helped me.

Suppose:

/users/42

Feels like:

Go to house number 42.

Specific location.

Specific target.

That’s how I remember params.

They point to something.


Express Example With Params

app.get(

"/users/:id",

(req,res)=>{

 console.log(
  req.params.id
 );

 res.send(
  "User found"
 );

}

);

If request is:

/users/42

Then:

req.params.id

gives:

42

That was my first “ohhh” moment.


URL Structure Breakdown


What Query Strings Are

Query strings look different.

They come after a question mark.

Example:

/products?category=books

Now:

category=books

is query data.

This usually feels like filtering.

Not identifying one specific thing.

That distinction matters.


I Think Of Query Like Preferences

This helped me.

Suppose you search products.

And say:

Only books.

Sort by price.

Show page 2.

Those are preferences.

Filters.

Modifiers.

That feels like query strings.


Express Example With Query

app.get(

"/products",

(req,res)=>{

 console.log(
  req.query.category
 );

 res.send(
  "Filtered products"
 );

}

);

Request:

/products?category=books

Then:

req.query.category

becomes:

books

Simple.


Params vs Query Comparison


This Comparison Made It Click

URL param:

/users/42

Meaning:

Which user?

Specific identifier.


Query string:

/users?role=admin

Meaning:

Filter users.

Different purpose.

That side-by-side helped me a lot.


User Profile Example

This made practical sense.

If I want one profile:

/users/42

Use param.

Because 42 identifies the user.

Makes sense.


Search Filter Example

If I want search options:

/products?category=books&sort=price

Use query.

Because I’m modifying results.

Not identifying one product.

That made the distinction feel natural.


Can You Use Multiple Query Values?

Yes.

This surprised me initially.

/products?category=books&page=2&sort=asc

Multiple query values.

Then:

req.query.page

works too.

Very useful.


Why I Used To Misuse Params

I used to do things like:

/products/books

for filtering.

And sometimes…

that wasn’t really an identifier.

It was acting like a filter.

Query may have made more sense.

That took me time.


When I’d Use Params

I usually think params when:

  • User ID

  • Product ID

  • Post ID

  • Anything uniquely identifying a resource

Like:

/posts/15

That feels like param territory.


When I’d Use Query

I think query when:

  • Search keywords

  • Filters

  • Sorting

  • Pagination

Like:

/posts?page=2

That feels like query territory.

Very different purpose.


Tiny Example With Both Together

This was cool when I first saw it.

/users/42?show=posts

Now you have:

Param:

42

Query:

show=posts

Both in same request.

And suddenly I realized…

they can work together.

That was nice.


Express Example Using Both

app.get(

"/users/:id",

(req,res)=>{

 console.log(
  req.params.id
 );

 console.log(
  req.query.show
 );

 res.send(
  "Done"
 );

}

);

Request:

/users/42?show=posts

Very practical.


One Mistake I Made

I thought params and query were interchangeable.

Not really.

You can misuse either.

But intent matters.

Identifier vs modifier.

That distinction helps.


Another Thing I Got Wrong

I thought query strings were only for search pages.

Not true.

They show up all over APIs.

Filters.

Pagination.

Feature flags.

Lots of places.

Worth noticing.


Small Practice Exercise

Try:

Route:

/users/:id

Read:

req.params.id

Then try:

/products?category=books

Read:

req.query.category

Those two examples teach most of the topic.

Honestly.


What Finally Made It Click

I stopped thinking:

Params and query are two random URL tricks.

And started thinking:

Params identify.

Query modifies.

That made everything simple.

Sometimes one sentence is enough.


Quick Recap

Params:

  • Inside route path

  • Usually identifiers

  • Access with req.params

Query:

  • After ?

  • Usually filters or modifiers

  • Access with req.query

That’s the foundation.


Conclusion

I used to confuse these all the time.

Now I usually ask one question:

Am I identifying something specific?

Use params.

Am I modifying results?

Use query.

That one check solves most cases.

If you remember one thing from this article, remember this:

Params identify what you want.
Query modifies how you want it.

That sentence made it click for me.

If you like this simple learning-style explanation,
I write more notes at
devwithsahil.hashnode.dev
and share progress on LinkedIn 🙂