Skip to content

Commit 4bdf82a

Browse files
Merge pull request #2523 from thiagomini/patch-6
docs(middlewares): add versioning integration docs
2 parents 16d3201 + 9761c1f commit 4bdf82a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

content/techniques/versioning.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,30 @@ app.enableVersioning({
292292
defaultVersion: VERSION_NEUTRAL
293293
});
294294
```
295+
296+
#### Middleware versioning
297+
298+
[Middlewares](https://docs.nestjs.com/middleware) can also use versioning metadata to configure the middleware for a specific route's version. To do so, provide the version number as one of the parameters for the `MiddlewareConsumer.forRoutes()` method:
299+
300+
```typescript
301+
@@filename(app.module)
302+
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
303+
import { LoggerMiddleware } from './common/middleware/logger.middleware';
304+
import { CatsModule } from './cats/cats.module';
305+
import { CatsController } from './cats/cats.controller';
306+
307+
@Module({
308+
imports: [CatsModule],
309+
})
310+
export class AppModule implements NestModule {
311+
configure(consumer: MiddlewareConsumer) {
312+
consumer
313+
.apply(LoggerMiddleware)
314+
.forRoutes({ path: 'cats', method: RequestMethod.GET, version: '2' );
315+
}
316+
}
317+
```
318+
319+
With the code above, the `LoggerMiddleware` will only be applied to the version '2' of `/cats` endpoint.
320+
321+
> info **Notice** Middlewares work with any versioning type described in the this section: `URI`, `Header`, `Media Type` or `Custom`.

0 commit comments

Comments
 (0)