Express: Optional trailing slash for top-level path Express: Optional trailing slash for top-level path express express

Express: Optional trailing slash for top-level path


It appears that a regular expression is the way to go:

app.get(/^[/]foo(?=$|[/])/, ...);


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?*', ...)


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.