Fix serverless functions: simplify API structure

- Replace complex Express-style handler with simple module.exports
- Add separate API endpoints for better organization
- Simplify vercel.json routing configuration
- Remove ES modules to use CommonJS for Vercel compatibility
- Add dedicated test API endpoint for debugging
This commit is contained in:
2025-08-21 13:32:53 +09:00
parent ec5da4db32
commit 08894eeb66
5 changed files with 73 additions and 43 deletions

40
api/files.js Normal file
View File

@@ -0,0 +1,40 @@
// 파일 API
module.exports = (req, res) => {
// CORS 헤더 설정
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}
// 샘플 파일 데이터
const files = [
{
id: 1,
title: '샘플 문서',
description: '데모용 파일입니다',
category: '문서',
tags: ['샘플', '테스트'],
created_at: new Date().toISOString(),
file_url: '#'
},
{
id: 2,
title: '이미지 파일',
description: '예시 이미지',
category: '이미지',
tags: ['샘플'],
created_at: new Date().toISOString(),
file_url: '#'
}
];
res.status(200).json({
success: true,
data: files,
message: '파일 목록을 성공적으로 불러왔습니다.'
});
};

View File

@@ -1,5 +1,5 @@
// Vercel Serverless 함수 핸들러 // Vercel Serverless 함수 핸들러
export default function handler(req, res) { module.exports = function handler(req, res) {
// CORS 헤더 설정 // CORS 헤더 설정
res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
@@ -27,7 +27,7 @@ export default function handler(req, res) {
// 루트 경로 // 루트 경로
if (url === '/' || url === '/api') { if (url === '/' || url === '/api') {
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.send(` res.end(`
<!DOCTYPE html> <!DOCTYPE html>
<html lang="ko"> <html lang="ko">
<head> <head>

21
api/test.js Normal file
View File

@@ -0,0 +1,21 @@
// 테스트용 간단한 API
module.exports = (req, res) => {
// CORS 헤더 설정
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}
// 간단한 응답
res.status(200).json({
success: true,
message: 'API가 정상 작동합니다!',
timestamp: new Date().toISOString(),
url: req.url,
method: req.method
});
};

View File

@@ -2,7 +2,6 @@
"name": "jaryo-file-manager", "name": "jaryo-file-manager",
"version": "2.0.0", "version": "2.0.0",
"description": "자료실 파일 관리 시스템 - Vercel Serverless", "description": "자료실 파일 관리 시스템 - Vercel Serverless",
"type": "module",
"scripts": { "scripts": {
"dev": "vercel dev", "dev": "vercel dev",
"build": "echo 'Build complete'", "build": "echo 'Build complete'",

View File

@@ -1,59 +1,29 @@
{ {
"version": 2, "version": 2,
"builds": [
{
"src": "api/simple.js",
"use": "@vercel/node"
}
],
"routes": [ "routes": [
{ {
"src": "/api/(.*)", "src": "/api/test",
"dest": "/api/simple.js" "dest": "/api/test.js"
}, },
{ {
"src": "/health", "src": "/api/files/public",
"dest": "/api/simple.js" "dest": "/api/files.js"
}, },
{ {
"src": "/(.*\\.(css|js|json|svg|png|jpg|jpeg|gif|ico|woff|woff2|ttf|eot|html))", "src": "/api/files",
"headers": { "dest": "/api/files.js"
"Cache-Control": "public, max-age=31536000, immutable" },
}, {
"src": "/(.*\\.(css|js|json|svg|png|jpg|jpeg|gif|ico|woff|woff2|ttf|eot))",
"dest": "/$1" "dest": "/$1"
}, },
{
"src": "/index\\.html",
"headers": {
"Cache-Control": "public, max-age=0, must-revalidate"
},
"dest": "/index.html"
},
{ {
"src": "/admin/(.*)", "src": "/admin/(.*)",
"dest": "/admin/$1" "dest": "/admin/$1"
}, },
{
"src": "^/$",
"dest": "/api/simple.js"
},
{ {
"src": "/(.*)", "src": "/(.*)",
"dest": "/index.html" "dest": "/index.html"
} }
], ]
"headers": [
{
"source": "/api/(.*)",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=0, s-maxage=86400"
}
]
}
],
"env": {
"NODE_ENV": "production"
}
} }