diff --git a/backend-node/src/app.ts b/backend-node/src/app.ts index 3b567a0a..465570ce 100644 --- a/backend-node/src/app.ts +++ b/backend-node/src/app.ts @@ -27,11 +27,11 @@ app.use(compression()); app.use(express.json({ limit: "10mb" })); app.use(express.urlencoded({ extended: true, limit: "10mb" })); -// CORS 설정 +// CORS 설정 - environment.ts에서 이미 올바른 형태로 처리됨 app.use( cors({ - origin: config.cors.origin.split(",").map((url) => url.trim()), - credentials: true, + origin: config.cors.origin, // 이미 배열 또는 boolean으로 처리됨 + credentials: config.cors.credentials, methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"], allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"], }) diff --git a/backend-node/src/config/environment.ts b/backend-node/src/config/environment.ts index 56478ae3..62fe0635 100644 --- a/backend-node/src/config/environment.ts +++ b/backend-node/src/config/environment.ts @@ -26,7 +26,7 @@ interface Config { // CORS 설정 cors: { - origin: string; + origin: string | string[] | boolean; // 타입을 확장하여 배열과 boolean도 허용 credentials: boolean; }; @@ -58,6 +58,26 @@ interface Config { showErrorDetails: boolean; } +// CORS origin 처리 함수 +const getCorsOrigin = (): string[] | boolean => { + // 개발 환경에서는 모든 origin 허용 + if (process.env.NODE_ENV === "development") { + return true; + } + + // 환경변수가 있으면 쉼표로 구분하여 배열로 변환 + if (process.env.CORS_ORIGIN) { + return process.env.CORS_ORIGIN.split(",").map((origin) => origin.trim()); + } + + // 기본값: 허용할 도메인들 + return [ + "http://localhost:9771", // 로컬 개발 환경 + "http://192.168.0.70:5555", // 내부 네트워크 접근 + "http://39.117.244.52:5555", // 외부 네트워크 접근 + ]; +}; + const config: Config = { // 서버 설정 port: parseInt(process.env.PORT || "3000", 10), @@ -82,8 +102,8 @@ const config: Config = { // CORS 설정 cors: { - origin: process.env.CORS_ORIGIN || "http://localhost:9771,http://192.168.0.70:5555,http://39.117.244.52:5555", - credentials: process.env.CORS_CREDENTIALS === "true" || true, + origin: getCorsOrigin(), + credentials: true, // 쿠키 및 인증 정보 포함 허용 }, // 로깅 설정