Code Self Study Forum

Best way to handle routing with parameters in Express?

I am following this tutorial on building a Node/Express app.

Now the issue I am having is how to route requests with parameters.

For example a route will look like this with a pagename and then 3-6 parameters


and in express I route that as follows:


This loads a controller function where I query for the parameters as follows:

    var param1 = [req.params.param1];
    var param2 = [req.params.param2];
    var param3 = [req.params.param3];

The problem I am running into now is that I get a
cannot Get error when a parameter is missing from the route.

For example
/page-name/ route with no parameters throws an error
/page-name/param1/ also throws an error because the other 2 parameters are not there.

What is the best way to deal with this issue when routing with Express?

1 Like

This answer provided by @frank on the Slack is best:

The question marks make the parameters optional.

1 Like