To add multiple optional route parameters in Node Express, we add question marks after the parameter placeholders.
For instance, we write
app.get("/articles/:year?/:month?/:day?", (req, res) => {
const year = req.params.year;
const month = req.params.month;
const day = req.params.day;
//...
});
to add a get route handler with the year
, month
and day
route parameters made option with ?
.
We get the route parameter values from the req.params
object.