Skip to content

Commit 4fb076a

Browse files
authored
docs(middlewares): add versioning integration docs
add docs explaining the integration between middlewares and versioning
1 parent 61bef8b commit 4fb076a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

content/middlewares.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,33 @@ export class AppModule {
177177

178178
> info **Hint** The `apply()` method may either take a single middleware, or multiple arguments to specify <a href="/middleware#multiple-middleware">multiple middlewares</a>.
179179
180+
#### Middleware versioning
181+
182+
The `MiddlewareConsumer` can also use the [versioning](https://docs.nestjs.com/techniques/versioning) metadata to configure the middleware for a specific route's version.
183+
184+
```typescript
185+
@@filename(app.module)
186+
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
187+
import { LoggerMiddleware } from './common/middleware/logger.middleware';
188+
import { CatsModule } from './cats/cats.module';
189+
import { CatsController } from './cats/cats.controller';
190+
191+
@Module({
192+
imports: [CatsModule],
193+
})
194+
export class AppModule implements NestModule {
195+
configure(consumer: MiddlewareConsumer) {
196+
consumer
197+
.apply(LoggerMiddleware)
198+
.forRoutes({ path: 'cats', method: RequestMethod.GET, version: '2' );
199+
}
200+
}
201+
```
202+
203+
With the code above, the `LoggerMiddleware` will only be applied to the version '2' of `/cats` endpoint.
204+
205+
> info **Notice** Middlewares work with any versioning type described in the linked section: `URI`, `Header`, `Media Type` or `Custom`.
206+
180207
#### Excluding routes
181208
182209
At times we want to **exclude** certain routes from having the middleware applied. We can easily exclude certain routes with the `exclude()` method. This method can take a single string, multiple strings, or a `RouteInfo` object identifying routes to be excluded, as shown below:

0 commit comments

Comments
 (0)