BE/NestJS NestJS : CORS - NestJS 공식문서: https://docs.nestjs.com/security/cors CORS는 서비스 하고 있지 않는 사이트에서 의도치 않는 접근을 막아주는 것이다. 프론트엔드에서 Api를 사용하려면 설정해주어야 한다. app.enableCors(); main.ts import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, OpenAPIObject, SwaggerModule } from '@nestjs/swagger'; import { AppModule } from './app.module'; import { HttpExceptionFilter } from './common/exceptions/http-exception.filter'; async function bootstrap() { const app = await NestFactory.create(AppModule); const PORT = process.env.PORT; app.useGlobalPipes(new ValidationPipe()); // validate app.useGlobalFilters(new HttpExceptionFilter()); const config = new DocumentBuilder() .setTitle('Cat example') .setDescription('cat api description') .setVersion('1.0.0') .addTag('cat') .build(); const document: OpenAPIObject = SwaggerModule.createDocument(app, config); SwaggerModule.setup('docs', app, document); // swagger api endpoint app.enableCors({ origin: true, // 접근 옵션 credentials: true, }); await app.listen(PORT); } bootstrap(); .enableCors의 option에서 origin의 값을 true로 설정하면 어느 곳에서든 접근을 허용한다는 의미이다. 개발할때는 보통 true로 설정해놓고 제한하려면 다음과 같이 아래처럼 url을 설정한다. origin: 'http://localhost:3000' 728x90 공유하기 URL 복사카카오톡 공유페이스북 공유엑스 공유 게시글 관리 구독하기Reality is also a Pizza Contents 당신이 좋아할만한 콘텐츠 NestJS : swagger 2022.06.02 NestJS : DTO 패턴 & 회원가입 service 2022.05.29 NestJS : DB Schema 설계 및 Validation 2022.05.28 NestJS : MongoDB 연결 및 환경 변수 설정 2022.05.28 댓글 0 + 이전 댓글 더보기