From 7b47b6cc2c3ed43d3e95e8ac5f13a0921af6686a Mon Sep 17 00:00:00 2001 From: patdelphi Date: Tue, 19 Aug 2025 17:37:22 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20Koyeb=20deployment:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=9B=91=E5=90=AC=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E5=92=8CCORS=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 11 +++++++---- server/index.cjs | 11 ++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5bcf6d1..dfe131d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,17 @@ # 使用官方Node.js运行时作为基础镜像 FROM node:18-alpine +# 安装pnpm +RUN npm install -g pnpm@9.0.0 + # 设置工作目录 WORKDIR /app -# 复制package.json和package-lock.json -COPY package*.json ./ +# 复制package.json和pnpm-lock.yaml +COPY package.json pnpm-lock.yaml ./ # 安装依赖 -RUN npm ci --only=production +RUN pnpm install --frozen-lockfile --prod # 复制应用代码 COPY . . @@ -24,4 +27,4 @@ ENV PORT=8000 EXPOSE 8000 # 初始化数据库并启动应用 -CMD ["sh", "-c", "npm run db:init && npm start"] \ No newline at end of file +CMD ["sh", "-c", "pnpm run db:init && pnpm start"] \ No newline at end of file diff --git a/server/index.cjs b/server/index.cjs index 6c9f1d0..ecafb77 100644 --- a/server/index.cjs +++ b/server/index.cjs @@ -42,7 +42,11 @@ app.use(helmet({ // CORS配置 app.use(cors({ origin: process.env.NODE_ENV === 'production' - ? ['http://localhost:5173', 'http://localhost:4173'] // 生产环境允许的域名 + ? [ + 'https://common-charyl-patdelphi-75adc386.koyeb.app', + 'http://localhost:5173', + 'http://localhost:4173' + ] // 生产环境允许的域名 : true, // 开发环境允许所有域名 credentials: true, methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], @@ -103,10 +107,11 @@ app.use('*', (req, res) => { app.use(errorHandler); // 启动服务器 -const server = app.listen(PORT, () => { - console.log(`🚀 服务器运行在 http://localhost:${PORT}`); +const server = app.listen(PORT, '0.0.0.0', () => { + console.log(`🚀 服务器运行在 http://0.0.0.0:${PORT}`); console.log(`📊 数据库文件: ${path.resolve('./numerology.db')}`); console.log(`🌍 环境: ${process.env.NODE_ENV || 'development'}`); + console.log(`🏥 健康检查: http://0.0.0.0:${PORT}/api/health`); }); // 优雅关闭