From 9b9eb7f321579886b6043a9511c13c7042ecc464 Mon Sep 17 00:00:00 2001 From: vibsin9322 Date: Thu, 21 Aug 2025 12:43:53 +0900 Subject: [PATCH] Fix Vercel deployment: add serverless compatibility and API routes --- api/index.js | 4 ++++ server.js | 41 +++++++++++++++++++++++------------------ vercel.json | 4 ++-- 3 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 api/index.js diff --git a/api/index.js b/api/index.js new file mode 100644 index 0000000..3a025de --- /dev/null +++ b/api/index.js @@ -0,0 +1,4 @@ +const app = require('../server'); + +// Vercel 서버리스 함수로 export +module.exports = app; diff --git a/server.js b/server.js index 9b854ae..cd8fb40 100644 --- a/server.js +++ b/server.js @@ -766,23 +766,28 @@ app.use((req, res) => { }); }); -// 서버 시작 -app.listen(PORT, () => { - console.log(`🚀 자료실 서버가 포트 ${PORT}에서 실행중입니다.`); - console.log(`📱 Admin 페이지: http://localhost:${PORT}/admin/index.html`); - console.log(`🌐 Main 페이지: http://localhost:${PORT}/index.html`); - console.log(`📊 API: http://localhost:${PORT}/api/files`); -}); +// Vercel 서버리스 환경을 위한 export +module.exports = app; -// 프로세스 종료 시 데이터베이스 연결 종료 -process.on('SIGINT', async () => { - console.log('\n📝 서버를 종료합니다...'); - await db.close(); - process.exit(0); -}); +// 로컬 개발 환경에서만 서버 시작 +if (process.env.NODE_ENV !== 'production' || process.env.VERCEL !== '1') { + app.listen(PORT, () => { + console.log(`🚀 자료실 서버가 포트 ${PORT}에서 실행중입니다.`); + console.log(`📱 Admin 페이지: http://localhost:${PORT}/admin/index.html`); + console.log(`🌐 Main 페이지: http://localhost:${PORT}/index.html`); + console.log(`📊 API: http://localhost:${PORT}/api/files`); + }); -process.on('SIGTERM', async () => { - console.log('\n📝 서버를 종료합니다...'); - await db.close(); - process.exit(0); -}); \ No newline at end of file + // 프로세스 종료 시 데이터베이스 연결 종료 + process.on('SIGINT', async () => { + console.log('\n📝 서버를 종료합니다...'); + await db.close(); + process.exit(0); + }); + + process.on('SIGTERM', async () => { + console.log('\n📝 서버를 종료합니다...'); + await db.close(); + process.exit(0); + }); +} \ No newline at end of file diff --git a/vercel.json b/vercel.json index aa4adf3..18babe3 100644 --- a/vercel.json +++ b/vercel.json @@ -2,14 +2,14 @@ "version": 2, "builds": [ { - "src": "server.js", + "src": "api/index.js", "use": "@vercel/node" } ], "routes": [ { "src": "/(.*)", - "dest": "/server.js" + "dest": "/api/index.js" } ], "env": {