The following:
app.get('/foo/start/:start/end/:end', blah.someFunc);
matches
/foo/start/1/end/4
but I want it to also match an optional parameter
/foo/start/1/end/4/optional/7
I've tried this:
app.get('/foo/start/:start/end/:end(/optional/:value)?', blah.someFunc);
but it doesn't match either of the above two examples. I think it's because I'm trying to give it a RegExp
when it's expecting something else?
Thanks.
Why don't you add another rule before the one you have, like this
app.get('/foo/start/:start/end/:end/optional/:value', blah.someFunc);
app.get('/foo/start/:start/end/:end', blah.someFunc);
It will be used before the one without the optional value.
If you want to use just one line try this:
app.get('/foo/start/:start/end/:end/optional?', blah.someFunc)
see the docs for an example.
If you are using Express 4.x Then I think its better to use array format for route.
For example I have route /service
which gives all service list and same route when used with id /service/id/:id
gives single service with id in the param.
app.get(['/service', '/service/id/:id'], function(req, res) {});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With