versioning 설정 적용
URI versioning 설정
- URI 에 버전을 포함하는 방법
- ex)
https://example.com/v1/route
const app = await NestFactory.create(AppModule);
// or "app.enableVersioning()"
app.enableVersioning({
type: VersioningType.URI,
});
await app.listen(3000);
const app = await NestFactory.create(AppModule);
app.enableVersioning({
type: VersioningType.HEADER,
header: 'Custom-Header',
});
await app.listen(3000);
const app = await NestFactory.create(AppModule);
app.enableVersioning({
type: VersioningType.MEDIA_TYPE,
key: 'v=',
});
await app.listen(3000);
Router versioning 적용
Controller 단위 적용
- controller 내 router 들에 적용하는 방법
@Controller({
version: '1',
path: 'animal'
})
export class CatsControllerV1 {
@Get('cats')
findAll(): string {
return 'This action returns all cats for version 1';
}
}
@Controller({
version: ['1', '2'],
path: 'animal'
})
export class CatsController {
@Get('cats')
findAll(): string {
return 'This action returns all cats for version 1 or 2';
}
}
Route 개별 적용
import { Controller, Get, Version } from '@nestjs/common';
@Controller('animal')
export class CatsController {
@Version('1')
@Get('cats')
findAllV1(): string {
return 'This action returns all cats for version 1';
}
@Version('2')
@Get('cats')
findAllV2(): string {
return 'This action returns all cats for version 2';
}
}
Middleware 를 통한 적용
- middleware 단에서 path 별로 버전을 명시하는 방법
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { LoggerMiddleware } from './common/middleware/logger.middleware';
import { CatsModule } from './cats/cats.module';
import { CatsController } from './cats/cats.controller';
@Module({
imports: [CatsModule],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(LoggerMiddleware)
.forRoutes({ path: 'animal/cats', method: RequestMethod.GET, version: '2' });
}
}
reference