I have these router declared in express.js, I wonder why the run user
got triggered when I open localhost:3000/myname/profile.
router.get('/:username', function(req, res, next)
{
console.log('run user')
});
router.get('/:username/profile', function(req, res, next)
{
console.log('run user profile')
});
I expect it won't,how to solve that? please anyone help me? Thank you in advance....
Just rearrange the code as shown below and your code should work fine.
router.get('/:username/profile', function(req, res, next)
{
console.log('run user profile')
});
router.get('/:username', function(req, res, next)
{
console.log('run user')
});
The issue is with the order in which the routes are defined, Since both the routes have /:username
when you hit http://localhost:3000/myname/profile.
, the first route is given preference since it matches the uri.
Also refer this stackoverflow post on express route naming and ordering - Node.js Express route naming and ordering: how is precedence determined?
Are you sure about that? I tried your code and it triggered run user profile
.May you should show the all code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.