Reputation: 194
I'm almost positive this bug resides in the way express deals with regex patterns in route definitions, but then again, it could reside in my pattern ( I'm still a regex noob, so please excuse me ). In my express route definition, I am trying to match a number in the route path:
const router = express.Router()
// pattern: ignore first capture (/)
router.route(/^(?:\/)(\d+)$/)
.get(...callback)
For now, that captured number can be of any length. So, things are working as expected when the url is: http://localhost:8000/1234
except for the fact that the full number is not captured - when I log the request params in my callback, I get: { '0', '4' }
. As you can see, I get the last digit:4
of the number:1234
. What is going wrong? I'm scratching my head vigorously here. When I test my regex in both regexr and the Node REPL: /^(?:\/)(\d+)$/.exec('1234')[1] === '1234'
, it seems to be matching correctly. Any help/insight would be greatly appreciated. Thanks.
[EDIT]: As suggested by @Tolsee, I upgraded my express package from v4.15.3
to v4.15.5
( the newest version ). This seemed to fix the issue; and now, my regex pattern is working for that route. I'm guessing that the problem lies with older express packages.
Upvotes: 0
Views: 210
Reputation: 31
You shouldn't use Regexp
here, It would be better with /:id/
(docs).
If you still wanna use Regexp
, try /^\d+(?:\/(?=$))?$/i
Hope it will be useful to you!
Upvotes: 1