import express, { type Express } from "express"; import fs from "fs"; import path, { dirname } from "path"; import { fileURLToPath } from "url"; // [수정] __dirname을 ES 모듈 방식에 맞게 안전하게 정의합니다. const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export function serveStatic(app: Express) { // [수정] 프로젝트 구조에 맞춰 빌드된 public 폴더의 경로를 정확히 잡습니다. // 보통 server 폴더의 한 단계 위(..)에 public이 위치합니다. const distPath = path.resolve(__dirname, "..", "public"); if (!fs.existsSync(distPath)) { console.error(`빌드 폴더를 찾을 수 없습니다: ${distPath}`); // 서버가 아예 죽지 않도록 에러를 던지는 대신 로그를 남깁니다. return; } // 1. 정적 파일(이미지, JS, CSS) 서빙 app.use(express.static(distPath)); // 2. [수정] 모든 요청을 index.html로 리다이렉트 (SPA 방식) // 비표준 문법인 "/{*path}" 대신 표준 문법인 "*"을 사용합니다. app.get("*", (req, res) => { // API 요청이 아닐 경우에만 index.html을 보냅니다. if (!req.path.startsWith("/api")) { res.sendFile(path.resolve(distPath, "index.html"), (err) => { if (err) { res.status(404).send("index.html 파일을 찾을 수 없습니다."); } }); } }); }