URL Parameters vs Query Strings in Express.js

URL / Path Parameters :-
Path params serves To identify a specific resource or a hierarchical relationship between resources for a particular purpose They serves as semantics or identifier expression . And they are mandatory for the routing .
For ex:-GET /authors/jk-rowling/books/harry-potter
The is not very flexible so it is only used for the params of particular location and it fetches only a single order .
Query Parameters :-
Query parameter are defined as the set of parameter (key-value pairs) that are attached at the end of URL using ? used to provide some kind of additional information to the server .In query params we can send a pair of key value pair send to server to let some kind of meta data .
Let's have a example :-https://example.com/path?name=Branch&products=[Journeys,Email,Universal%20Ads]
In above example :-
name with value Branch
products with value [Journeys,Email,Universal%20Ads
Query params are Suitable for :- Searching ,Pagination or passing optional data .This method is more flexible and also allows multiple parameters without altering the URL structure.
Difference btw them
Path Params | Query Params |
Suitable for the Particular or _Specific route . | Suitable for the optional data . |
Only have a single parameter | Can have a multiple parameter . |
Used in URL after /: | Used in URL by pasting key value pair after ? |
Accessing params in Express
We can access params in express using / in express
for ex:- http://instame/profiles/: id In this case we going hierarchical to we are going to the profile by given id in this And it will exactly go this specific id .
Accessing query params in Express
We can access the query params in the express is giving the key value pair after the ? in the URL .
For ex:- http://instame/profiles?status="online" Let's see this example in this we are passing a key value pair in which value is online means it gives us all the profile which are online at this time .
When to use params vs query
Let's answer this when to use whom it it covered in the above content but we guve a summary of it Use path parameters when you need to identify a specific resource, and use query parameters when you want to filter, sort, or modify that resource.


