Quiz

  1. 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):

    
    app.get('/people-born-in/:monthNumber', async (req, res) => {
        const monthNumber = req.params.monthNumber;
        ...
        let all = await people.find({}).toArray();
        let chosen = peopleBornInMonth(all, monthNumber);
        let descriptions = chosen.map(personDescription);
        ...
        return res.render('list.ejs',
                          {listDescription: `${num} people born in ${monthNames[monthNumber]}`,
                           list: descriptions});
    });
    
    

    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:

    
    app.get('/people-born-in-month/', (req, res) => {
        let monthNumber = req.query.month;
        ...
        console.log('monthNumber', monthNumber, 'redirecting');
        res.redirect('/people-born-in/'+monthNumber);
    });
    
    

    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.

  2. 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 versus req.query

  3. Could we talk a little more about the handler function that searches the database?

    Sure. Let's look through all that handler code