From aef3acb6dc65b2dfdc1049b0557640544f8dbc3a Mon Sep 17 00:00:00 2001 From: golc <2483469113@qq.com> Date: Wed, 4 Mar 2026 09:35:08 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B9=E8=BF=9B=20Vercel=20serverless?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E5=92=8C=E8=B7=AF=E5=BE=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 vercel.json 构建配置 - 使用 path.join 处理文件路径 - 添加更详细的日志输出 - 改进错误处理和回退机制 - 添加 installCommand 确保正确构建 Co-Authored-By: Claude Sonnet 4.6 --- api/index.ts | 25 +++++++++++++++++++++---- vercel.json | 5 +++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/api/index.ts b/api/index.ts index 70222f9..f68f0e1 100644 --- a/api/index.ts +++ b/api/index.ts @@ -4,6 +4,7 @@ import { ValidationPipe } from '@nestjs/common'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import * as express from 'express'; import * as bodyParser from 'body-parser'; +import { join } from 'path'; // 缓存 app 实例,避免每次冷启动都重新创建 let cachedApp: any; @@ -13,11 +14,22 @@ async function bootstrap() { const expressApp = express(); // 动态导入模块,支持构建后的路径 - const { AppModule } = await import('../dist/app.module.js'); + let AppModule; + try { + // 尝试从 dist 目录加载 + const distPath = join(process.cwd(), 'dist', 'app.module.js'); + AppModule = (await import(distPath)).AppModule; + } catch (error) { + console.error('Failed to load from dist:', error.message); + // 回退到 src 目录 + const srcPath = join(process.cwd(), 'src', 'app.module.ts'); + AppModule = (await import(srcPath)).AppModule; + } const app = await NestFactory.create( AppModule, new ExpressAdapter(expressApp), + { logger: ['error', 'warn', 'log'] } ); // 配置 CORS @@ -41,8 +53,9 @@ async function bootstrap() { // 尝试加载全局过滤器和拦截器(如果存在) try { - const { HttpExceptionFilter } = await import('../dist/common/filters/http-exception.filter.js'); - const { TransformInterceptor } = await import('../dist/common/interceptors/transform.interceptor.js'); + const distCommonPath = join(process.cwd(), 'dist', 'common'); + const { HttpExceptionFilter } = await import(join(distCommonPath, 'filters', 'http-exception.filter.js')); + const { TransformInterceptor } = await import(join(distCommonPath, 'interceptors', 'transform.interceptor.js')); app.useGlobalFilters(new HttpExceptionFilter()); app.useGlobalInterceptors(new TransformInterceptor()); } catch (error) { @@ -74,6 +87,8 @@ async function bootstrap() { await app.init(); cachedApp = expressApp; + + console.log('NestJS app initialized successfully'); } return cachedApp; } @@ -81,13 +96,15 @@ async function bootstrap() { // Vercel Serverless handler export default async function handler(req: any, res: any) { try { + console.log(`Request: ${req.method} ${req.url}`); const app = await bootstrap(); app(req, res); } catch (error) { console.error('Handler error:', error); res.status(500).json({ error: 'Internal Server Error', - message: error.message + message: error.message, + stack: process.env.NODE_ENV === 'development' ? error.stack : undefined }); } } diff --git a/vercel.json b/vercel.json index 52d7759..de27626 100644 --- a/vercel.json +++ b/vercel.json @@ -2,7 +2,7 @@ "version": 2, "builds": [ { - "src": "package.json", + "src": "api/index.ts", "use": "@vercel/node" } ], @@ -11,5 +11,6 @@ "src": "/(.*)", "dest": "/api/index.ts" } - ] + ], + "installCommand": "pnpm install && pnpm run build" }