This is working as expected. In Express 4 all middleware/routes are executed in the order that they are added to your app, so the path for your second router.get()
is tested and matches because :user_id
could be any kind of token, numeric or otherwise (basically any character that isn't /
).
You can fix this at least in a couple of different ways:
- Don't call
next()
in your firstrouter.get()
- Use a RegExp instead of a string to ensure that
user_id
only matches a numeric value. For example:router.get(/^\/(\d+)$/, function() {
. However then you have to access the param via regexp group index instead of by a name:var user_id = req.params[0]
.