New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🤔 Middleware seems to run earlier than wildcard routing #207
Comments
Routes are executed in order #204 app.Group("/api", Auth())
// equals
app.Use("/api", Auth()) This means that every route starting with If I'm correct, you only want to execute the middleware handler if the child route exists? |
@Fenny Yes ! Since child route doesn't exist, my expectation is not to execute any middleware, but to use a generic route to return 404 directly。 |
Our routing logic is based on the express framework and works the same.
const express = require('express')
const app = new express()
app.use('/api', (req, res, next) => {
console.log('middleware!')
next()
})
app.get('/api/login', (req, res, next) => {
res.send("Welcome user!")
})
app.use('*', (req, res) => {
res.sendStatus(404);
})
app.listen(3001) When using If you really want to execute the middleware only when a route is found, I suggest attaching the middleware to a specific route directly. app.Post("/login", JWT(), Login()) |
Thanks! It seems to be a feature rather than a problem |
Question description
Middleware seems to run earlier than wildcard routing,I use a custom middleware in router.Group,finally register a Not found route。
when i visit the "http://127.0.0.1:8080/api/v1/anyPathNotExist",server still exec the JWTAuth() first,But my expectation is that the return route doesn't exist
Code snippet (optional)
The text was updated successfully, but these errors were encountered: