Middleware for all routes not working with pathless controller and methods
See original GitHub issueBug Report
Current behavior
A Middleware configured with forRoutes('*')
is not called with a controller without path, such like @Controller()
, and also a method without any path, like @Get()
. It works with a path in either of them however. The reason behind a pathless controller is I already have a app.setGlobalPrefix('api/...')
in my app and I am putting up a controller for that path of global prefix, so no need for a path.
Input Code
//Basically like
NestFactory.create(UserAPIModule, adapter)
.then(app => {
app.setGlobalPrefix('api/user');
app.enableCors();
return app.init();
})
// Then...
@Module({
imports: [UserModule],
})
export class UserAPIModule {
public configure(consumer: MiddlewareConsumer) {
consumer.apply(MyMiddleware).forRoutes('*');
}
}
@Injectable()
export class MyMiddleware implements NestMiddleware {
public use(req: Request, res: Response, next: Function) {
console.log('THIS IS NOT CALLED');
next();
}
}
@Module({
imports: []
controllers: [UserController],
})
export class UserModule {}
@Controller()
export class UserController {
constructor() {}
@Get()
public async getUser() {
console.log('this is called without middlware');
return 'Users object';
}
}
Expected behavior
Middlewares should also be called on pathless controllers due to the meaning of the wildcard
Possible Solution
Environment
Nest version: 6.2.4
For Tooling issues:
- Node version: XX
- Platform:
Others:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Nestjs: middleware for all routes except /auth - Stack Overflow
In the documentation it says that I can add all the controllers or paths of the routes where I want the middleware, but...
Read more >React-router nested pathless routes as middleware : r/reactjs
Is it good practice to use multiple nested patheless routes (returning Outlet or Navigate depending on logic) to act as middleware ?
Read more >Middleware | NestJS - A progressive Node.js framework
Middleware is a function which is called before the route handler. Middleware functions have access to the request and response objects, and the...
Read more >Express Tutorial Part 4: Routes and controllers - MDN Web Docs
Router middleware as it allows us to group the route handlers for a particular part of a site together and access them using...
Read more >Match - React Router: Declarative Routing for React.js
A match object contains information about how a <Route path> matched the URL. match ... If a Route does not have a path...
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 Free
Top 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
I’m also facing the same issue. In my use case if you put a trailing slash (
http://localhost:3000/api/user/
) while accessing the api then middleware is invoked but if you remove the trailing slash (http://localhost:3000/api/user
) it is bypassed.This has been clarified in the documentation (as part of the v7 release) https://docs.nestjs.com/middleware#route-wildcards