diff --git a/numerology.db b/numerology.db index e69ef0c..bb43ac2 100644 Binary files a/numerology.db and b/numerology.db differ diff --git a/numerology.db-shm b/numerology.db-shm deleted file mode 100644 index 7ee26d2..0000000 Binary files a/numerology.db-shm and /dev/null differ diff --git a/numerology.db-wal b/numerology.db-wal deleted file mode 100644 index 34a5336..0000000 Binary files a/numerology.db-wal and /dev/null differ diff --git a/server/database/index.cjs b/server/database/index.cjs index d27e5cb..6eff2c3 100644 --- a/server/database/index.cjs +++ b/server/database/index.cjs @@ -15,6 +15,12 @@ class DatabaseManager { // 初始化数据库连接 init() { try { + // 确保数据库目录存在 + const dbDir = path.dirname(this.dbPath); + if (!fs.existsSync(dbDir)) { + fs.mkdirSync(dbDir, { recursive: true }); + } + // 创建或连接到SQLite数据库 this.db = new Database(this.dbPath); diff --git a/server/index.cjs b/server/index.cjs index 30249f7..bc896a1 100644 --- a/server/index.cjs +++ b/server/index.cjs @@ -43,7 +43,7 @@ app.use(helmet({ app.use(cors({ origin: process.env.NODE_ENV === 'production' ? (origin, callback) => { - // 允许Koyeb域名、localhost和环境变量指定的域名 + // 生产环境的严格检查 const allowedOrigins = [ 'http://localhost:5173', 'http://localhost:4173', diff --git a/src/lib/localApi.ts b/src/lib/localApi.ts index cf1b980..0ca113e 100644 --- a/src/lib/localApi.ts +++ b/src/lib/localApi.ts @@ -1,8 +1,14 @@ // 本地API客户端 // 替代Supabase客户端,提供相同的接口 +// 强制在开发环境使用正确的后端地址 const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || - (import.meta.env.PROD ? 'https://your-app.koyeb.app/api' : 'http://localhost:3001/api'); + (import.meta.env.DEV ? 'http://localhost:3001/api' : `${window.location.origin}/api`); + +// 调试信息 +console.log('API_BASE_URL:', API_BASE_URL); +console.log('import.meta.env.DEV:', import.meta.env.DEV); +console.log('import.meta.env.PROD:', import.meta.env.PROD); interface ApiResponse { data?: T; @@ -69,7 +75,32 @@ class LocalApiClient { }, }); - const data = await response.json(); + // 检查响应是否为空或非JSON格式 + const contentType = response.headers.get('content-type'); + if (!contentType || !contentType.includes('application/json')) { + if (!response.ok) { + return { + error: { + code: 'HTTP_ERROR', + message: `HTTP ${response.status}: ${response.statusText}`, + }, + }; + } + // 如果是成功响应但不是JSON,返回空数据 + return { data: {} as T }; + } + + let data; + try { + data = await response.json(); + } catch (jsonError) { + return { + error: { + code: 'JSON_PARSE_ERROR', + message: '服务器返回了无效的JSON格式', + }, + }; + } if (!response.ok) { return { @@ -80,7 +111,7 @@ class LocalApiClient { }; } - return { data: data.data }; + return { data: data.data || data }; } catch (error) { console.error('API请求错误:', error); return {