Router runs middleware on routes, which are not in array.
See original GitHub issuenode.js version: v10.16.3
npm/yarn and version: 6.13.4
@koa/router
version: 8.0.5
koa
version: 2.11.0
Code sample:
const Router = require('@koa/router')
const validate = require('../middlewares/validator')
const guard = require('../middlewares/guard')
const AccountController = require('../controllers/account')
const router = new Router({
prefix: '/account'
})
router.use(router.routes(), guard.ip.filter())
router.use([
'/signin',
'/update',
'/password/change',
'/password/forgot',
'/password/reset',
'/email/change/request',
'/email/change/confirm',
'/email/verify'
], validate())
router.use([
'/signout',
'/',
'/update',
'/password/change',
'/email/change/request'
], guard.user.passport('jwt'), guard.csrf())
// Routes
router.post('/signin', guard.user.passport('local'), AccountController.signIn)
router.delete('/signout', AccountController.signOut)
router.get('/', AccountController.profile)
router.post('/update', AccountController.update)
router.post('/password/change', AccountController.password.change)
router.post('/password/forgot', AccountController.password.forgot)
router.post('/password/reset', AccountController.password.reset)
router.post('/email/change/request', AccountController.email.change.request)
router.post('/email/change/confirm', AccountController.email.change.confirm)
router.post('/email/verify', AccountController.email.verify)
module.exports = router
Expected Behavior:
Expected, that route ‘/’ will effect exactly for route ‘/’, when using:
router.use([ '/signout', '/', '/update', '/password/change', '/email/change/request' ], guard.user.passport('jwt'), guard.csrf())
So that passport middleware should run exactly on those routes, which are is this array.
Actual Behavior:
Unfortunately route ‘/’ in the array of routes above effects on any other route, even on '/signin'
, which is not in array for passport middleware.
Additional steps, HTTP request details, or to reproduce the behavior or a test case:
Issue Analytics
- State:
- Created 4 years ago
- Comments:9
Top Results From Across the Web
Using middleware - Express.js
Route handlers enable you to define multiple routes for a path. The example below defines two routes for GET requests to the /user/:id...
Read more >Use specific middleware in Express for all paths except a ...
I would add checkUser middleware to all my paths, except homepage. app.get('/', routes.index); app.get('/account', checkUser, routes.account);.
Read more >Middleware - Sails.js
Technically, much of the code you'll write in a Sails app is middleware, in that runs in between the incoming request and the...
Read more >Routing - Slim Framework
The Slim Framework's router is built on top of the Fast Route component, and it is remarkably fast and stable. While we are...
Read more >Complete Guide to Express Middleware - Reflectoring
Middleware functions in Express are of the following types: Application-level middleware which runs for all routes in an app object; Router ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Another possibility is to declare different routers for your different concerns. In a specific router, Koa-router won’t execute a middleware if there’s no matching route. (I’d like to change this behavior in #44 though).
The problem in #90 is that both routes match the first middleware, so the behavior I’m mentioning doesn’t work for this case. What seems to work though is reversing the order of your routes. I’ll leave a message in the other issue.