CORS origin 처리 단순화

This commit is contained in:
hyeonsu 2025-09-04 16:29:57 +09:00
parent 272385a120
commit 71b509b11e
2 changed files with 26 additions and 6 deletions

View File

@ -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"],
})

View File

@ -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, // 쿠키 및 인증 정보 포함 허용
},
// 로깅 설정