I am pretty sure that the issue lies in how express handles regex patterns in route definitions, although it might also be related to my pattern (I'm still new to regex, so please bear with me). In my express route definition, I am attempting to match a number in the route path:
const router = express.Router()
// pattern: ignore first capture (/)
router.route(/^(?:\/)(\d+)$/)
.get(...callback)
Currently, the captured number can have any length. Everything works fine when the URL is: http://localhost:8000/1234
, except for the fact that the entire number is not captured - when I check the request params in my callback function, I see: { '0', '4' }
. It seems like only the last digit:4
of the number:1234
is being captured. What could be going wrong? I'm puzzled by this. When I test my regex using both regexr and the Node REPL:
/^(?:\/)(\d+)$/.exec('1234')[1] === '1234'
, it appears to match correctly. Any help or insight would be greatly appreciated. Thank you.
[EDIT]: Following the suggestion of @Tolsee, I updated my express package from version 4.15.3
to version 4.15.5
(the latest version). This resolved the issue; now, my regex pattern functions correctly for that route. It seems like the problem was related to older express packages.