in express documentation here https://expressjs.com/en/guide/routing.html, it says Query strings are not part of the route path. Could somebody kindly explain this to me? I know it may be an obvious question, but I really don't know. Thank you in advance.
-
Because they come at the end of the URL after a question mark... – vicatcu Jun 07 '18 at 22:22
-
What's the special of the question mark? In express documentation, I see $ could be a part of path string, why not question mark? – Jane Jun 07 '18 at 22:31
-
Please refer question https://stackoverflow.com/questions/43208713/express-get-route-will-not-work-with-parameters for more clarifications. Hope it helps! – vCillusion Jun 08 '18 at 02:50
-
Sorry, I didn't really get it. I just felt that the implementations for route path and query string are different. But what are essential difference? – Jane Jun 08 '18 at 18:27
1 Answers
Query strings are used to pass data to the request, mostly for get requests.
They are denoted always by the ? symbol, which is why it is not allowed in paths. For example, a website may have a search page where you can search with a keyword /search?searchterm=somedata
where search term is the key for the value and somedata is the value itself.
They are not included because it is expected that the data is always being passed to that specific route, and if you wanted to create a different route for something in the query, you would most likely use params, with a route string like "/xyz/:somedata"
where somedata
can be accessed from inside the req.params.somedata
object.
EDIT: The best practice for API design is that path params are used to identify a specific resource or resources, while query parameters are used to sort/filter those resources.
I hope that makes sense.
-
You are saying they are for different purposes. But to me, I feel they are pretty close. Using either of them, we could pass variable from clients to servers. Any fundamental difference? – Jane Jun 08 '18 at 18:30
-