'How to exclude all routes except one in NestJs?
Let's say that I have controller with two routes:
@Controller('events')
export class EventController {
@Get('my')
async getMyEvents() {
return "A"
}
@Get(':eventId')
async getEvent(@Param('eventId', ParseUUIDPipe) eventId: string) {
return "B"
}
}
and I need to exclude all routes except one which have param:
export class EventModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(AuthMiddleware)
.exclude({path: 'api/events/:eventId', method: RequestMethod.GET})
.forRoutes(EventController)
}
}
but it doesn't work, it also exclude route api/events/my, so how to avoid that ?
Solution 1:[1]
add the routes in exclude, you want to exclude and the rest in forRoutes
export class EventModule implements NestModule { configure(consumer: MiddlewareConsumer) { consumer .apply(AuthMiddleware) .exclude("*") .forRoutes({path: 'api/events/:eventId', method: RequestMethod.GET}) } }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | A A Maruf |
