Categories
JavaScript Answers

How to add optional splat param with Node Express.js routing?

Spread the love

To add optional splat param with Node Express.js routing, we use a question mark.

For instance, we write

router.get("/path/:id*?", (req, res, next) => {
  res.render("page", { title: req.params.id });
});

to call get to with the path of the route.

We make id an option parameter with ?.

And we get the id value with req.params.id.

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 *