Skip to content

Commit 9761c1f

Browse files
authored
docs(techniques): move middleware section
move versioning-middleware integration docs to versioning page.
1 parent 4fb076a commit 9761c1f

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

content/middlewares.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -177,33 +177,6 @@ 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-
207180
#### Excluding routes
208181

209182
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:

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)