From 4084b8d2cf46e9fad479abb95977e348f911890b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Thu, 1 Sep 2022 16:28:45 +0900 Subject: [PATCH] =?UTF-8?q?NestJS=20=EC=88=98=EC=A0=952..?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 8b082d9..9c50cf0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,37 @@ //참고 : https://wikidocs.net/book/7059 +import { HttpException, HttpStatus } from '@nestjs/common' import { NestFactory } from '@nestjs/core' import { AppModule } from './app.module' async function bootstrap() { const app = await NestFactory.create(AppModule) + //Enable All CORS Requests : https://docs.nestjs.com/security/cors + app.enableCors({ + origin: (origin, callback) => { + //origin URL이 허용된 경우 또는 orgin자체가 없는 경우(postman tool) 통과 + if (process.env.CORS_ALLOW_ORIGINS.indexOf(origin) !== -1 || !origin) { + console.log('Allowed Origin URL: ' + origin) + callback(null, true) + } else { + callback( + new HttpException( + { + status: HttpStatus.FORBIDDEN, + error: origin + '에서는 접근이 허용되지 않습니다.' + }, + HttpStatus.FORBIDDEN + ) + ) + } + }, + methods: process.env.CORS_ALLOW_METHOD, + credentials: true + }) await app.listen(3000, function () { - console.log('listening on port 3000') + console.log( + '[CORS-enabled->npm install -g webpack webpack-cli] web server listening on port 3000' + ) }) } bootstrap()