Express.js routing: optional splat param?
I just had the same problem and solved it. This is what I used:
app.get('path/:required/:optional*?', ...)
This should work for path/meow
, path/meow/voof
, path/meow/voof/moo/etc
...
It seems by dropping the /
between ?
and *
, the last /
becomes optional too while :optional?
remains optional.
This works for /path and /path/foo on express 4, note the *
before ?
.
router.get('/path/:id*?', function(req, res, next) {
res.render('page', { title: req.params.id });
});