-2

Why is a URL that is recognizable to Express.js, not like the usual URLs we see?

For example: Express.js will recognize this URL (http://localhost:3000/things/0) where id=0 if I make this GET request:

app.get('/things/:id', (req, res) => {
   // Do somethihng
});

But this URL (http://localhost:3000/things/?id=0) which is more like the usual URLs we see won't work for the same GET request above.

  • 1
    What do you mean by `usual URLs`? – prosach Jan 09 '22 at 03:39
  • Express allows you to specify a path in several ways: http://expressjs.com/en/4x/api.html#path-examples What you have in your example is just one template for matching URLs to parameters. It's for convenience. – Brad Jan 09 '22 at 03:48
  • In Express, `/things/:id` will allow you to make a request to `http://localhost:3000/things/0` and access the `0` within the request in `req.params.id`. If the URL is `http://localhost:3000/things/?id=0`, the you declare your Express route as `/things` and you access the `id` in `req.query.id`. Query parameters are usually optional so you don't put them in your route declaration. Instead Express just parses any query parameters that are present and puts then in `req.query` for you to examine. – jfriend00 Jan 09 '22 at 06:27
  • Does this answer your question? [Node.js: Difference between req.query\[\] and req.params](https://stackoverflow.com/questions/14417592/node-js-difference-between-req-query-and-req-params) – prosach Jan 10 '22 at 03:17

2 Answers2

0

(http://localhost:3000/things/?id=0) which is more like the usual URLs

It's framework and it doesn't do what you and I think. It has set of rules. Talking about best practices for RESTFUL API, design is that path params(/:id) are used to identify a specific resource or resources, while query parameters(?id) are used to sort/filter those resources. So, for /things/?id=0, We should use query parameters.

we see won't work for the same GET request above.

It will not work that way. In order to get query parameters use something like

app.get('/things', (req, res) => {
   let id = req.query.id;
// here you can get id.
});

QUERY PARAMETERS: How to get GET (query string) variables in Express.js on Node.js?

ABOUT URL: https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL

prosach
  • 312
  • 2
  • 14
0

So, "normal URLs" is apparently in the eye of the beholder as there are really just different styles, neither being any more normal than the other.

There are a couple ways to pass parameters in a URL.

#1 - You can embed it in the path of the URL

https://someserver/things/0
https://contacts.google.com/person/c3429579852656514228

This is referred to as a RESTFUL design. These URLs contain a noun and then an id that identifies which of those nouns the URL refers to.

#2 - You can put the variable part of the URL in a query parameter

For those same two URLs above, that could look like this:

https://someserver/things?id=0
https://contacts.google.com/person?id=c3429579852656514228

There are valid reasons for both designs depending upon circumstances and there are even cases where you combine the two (add optional query parameters to the restful API designs in option #1. Neither one is "normal" - they are different ways to design your URLs.

Express allows you to use either one. For restful parameters that are in the path of the URL, you use the :id syntax and you access the value in req.params.id:

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

For query parameters, you don't define them in the express route path. Instead, Express parses any query parameters that exist on any route and makes them available in req.query.

// expecting /things?id=789&sort=alpha
app.get('/things', (req, res) => {
   console.log(req.query.id);            // "789"
   console.log(req.query.sort);          // "alpha"
   res.send(req.query.id);
});

Any query parameter that is present in the URL will be added to the req.query object.

Note that query parameters are often considered optional and thus you don't code them into your route definition. If you want to require a specific query parameter, then you have to check if it's there and, if not, then provide some error response or call next() to continue to routing to other request handlers.


FYI, there's another style that often uses client-side code to help build that page and puts URL arguments in a hash tag too, but I'll leave that for another day.

jfriend00
  • 683,504
  • 96
  • 985
  • 979