Express: Optional trailing slash for top-level path
I know this is an old question, but I had the same issue and I came up with this:
app.get(['/foo', '/foo/*'], ...);
This will match /foo
, /foo/
, and anything under /foo/...
. I think this is a more readable solution than a regular expression and it communicates clearly what is intended.
It appears that a regular expression is the way to go:
app.get(/^[/]foo(?=$|[/])/, ...);
I had this issue as well and I have to agree that the Regular expression works the best, however with a slight improvement:
app.get('/foo([/].*)?', ...);
This would match:
/foo
/foo/
/foo/what/ever/you/put/after
The /foo/:dummy?*
doesn't really take the optional /
after foo
into account and /foo(/:dummy*)?
doesn't really match correctly.
/foo(/:dummy)?*
kinda works but creates an extra variable as it has both path
and the *
in separate variables, which is inexpedient in this case.
So personally I would stick with the Regular expression.
If you want to match them in one pattern:
app.get('/foo(/bar(/baz)?)?', ...)
The default Express behaviour of allowing an optional /
at the end applies.
EDIT: how about this?
app.get('/foo/:dummy?*', ...)