Categories
JavaScript Answers

How to add multiple optional route parameters in Node Express?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *