I would like to get URLs of the form (strictly) /forumId/slug/
with Express' router.get()
. Additional tokens should result in a 404.
I came up with this path route: /:forumId([^?\/]{0,}):parameters1?/:slug([^?\/]{0,})?:parameters2?
which according to express-route-tester
behaves as expected.
However Express seems to also be catching paths with extra tokens after the slug. Directly passing a Javascript regular expression instead of a string has the same effect.
Here is the regex I used: /^\/(\d+)(?:\?[^\?\/]{0,})?(?:$|\/([^\?\/]{0,})(?:\?[^\?\/]{0,})?\/?$)/
Examples of strings that should match (those work):
/7721
/7721/
/7721?page=2
/7721/ForumTitle/
/7721/AnotherForumTitle?test/
/7721/YetAnotherForumTitle?page=2
/7721?page=3/ForumTitle?page=2
/7721?page=3/ForumTitle?page=2/
Example of strings that shouldn't match:
/7721?page=3/ForumTitle?page=2/threadId
Express uses path-to-regexp
to parse the string and according to the docs (https://www.npmjs.com/package/path-to-regexp#user-content-usage), the 'end' option is set to true by default which seems to corroborate that the match should stop right after the slug.
What am I missing?
The version of Express is 4.16.4.
UPDATE: removing the part about the query parameters (?page=2) in the regex solved the problem. Why is that so? Was my regex flawed (I tried spotting possible greedy matches but couldn't find one) or is this expected from path-to-regexp
?