Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get method with colon and question mark in server path

Follow up question: What is the code "res.json(false);" doing? Doesn't that print out false on the page instead of showing the data I want?

I'm looking at the following sample code. I understand that .get( is the method and /:characters? is the server path. In this search, what is the point of the colon and the question mark in the path? Shouldn't the question mark come before characters because it is a query?

app.get('/:characters?', function (req, res) {
    var chosen = req.params.characters;

    if (chosen) {
        console.log(chosen);

        for (var i = 0; i < characters.length; i++) {
            if (chosen === characters[i].routeName) {
                res.json(characters[i]);
                return;
            }
        }

        res.json(false);
    } else {
        res.json(characters);
    }
});
like image 820
Frank Smith Avatar asked Sep 27 '16 18:09

Frank Smith


1 Answers

In this case the question mark signifies an optional parameter "characters". This allows for an endpoint that MAY have a value or not. They are then testing against that parameter to see if it was included. If so they will iterate through the "characters" object and return any matching entry to the endpoint the user specified.

like image 135
ideascomealive Avatar answered Oct 13 '22 19:10

ideascomealive