Quiz
- Why is the month number not a parameterized endpoint in this reading, but it seems to be in the People Born in a Month code example?
Good question! The two kinds of urls that include the month number are these:
- /people-born-in/6
- /people-born-in-month/?month=6
Very similar, and not just in the name of the endpoint. Maybe I should name those more differently, like this:
- /fred/6
- /george/?month=6
the
fred
endpoint is a parameterized endpoint, and matches with a handler like this (see people-born-in-a-month):The
george
endpoint is not parameterized. Instead, it handles a query string, which is the name=value pairs after the question mark. It corresponds to:We could handle this request right here, but in this case we decide to use a redirect to the other endpoint. That's the URL we want people to bookmark, etc.
- Could we talk more about redirects? Could we talk more about the visualization of redirects? What is the exact difference between /people-born-in/6 URL and the /people-born-in-month/?month=6 url?
Redirects are used when there's an endpoint we would prefer the user to access, because it's more "authoritative" or canonical, better for SEO, etc.
We would not use a redirect just for programming convenience, because there is a cost in terms of network traffice and such.
They differ in how the URL is parsed and where Express puts the information:
req.params
versusreq.query
- Could we talk a little more about the handler function that searches the database?
Sure. Let's look through all that handler code