Jesse Jesse - 2 years ago 215
Node.js Question

Express.js routing: optional spat param?

I have a route that looks like this:

// do something when there is no splat
} else {
// do something with splat

however, this doesn't work - if I call
it hits the route, but if I call
, it doesn't.

Is it possible to have an optional splat param, or do I have to use a regex to detect this?


to be clearer, here are the requirements I'm trying to achieve:

  • the first and second params are required

  • the first param is static, the second is a named param.

  • any number of optional additional params can be appended and still hit the route.

Answer Source

This works for /path and /path/foo on express 4, note * before ?

router.get('/path/:id*?', function(req, res, next) {
    res.render('page', { title: });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download