From 78e73fcb8f08395bd7ffd3ae9758c7913de35f0d Mon Sep 17 00:00:00 2001 From: golc <2483469113@qq.com> Date: Wed, 4 Mar 2026 09:39:57 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=85=A8=E4=BF=AE=E5=A4=8D=20V?= =?UTF-8?q?ercel=20=E9=83=A8=E7=BD=B2=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 核心修复: - 从 src 导入模块而非 dist(让 TypeScript 编译器处理) - vercel.json 路由指向构建后的 dist/api/index.js - 简化导入逻辑,移除动态路径拼接 - 本地构建测试通过 技术细节: - api/index.ts 从 ../src/app.module 导入 - NestJS 构建会将其编译到 dist/api/index.js - Vercel 执行构建后直接使用编译结果 - CORS 设置为 origin: true 允许所有来源 Co-Authored-By: Claude Sonnet 4.6 --- api/index.ts | 30 +++++++----------------------- vercel.json | 7 +++---- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/api/index.ts b/api/index.ts index 8af7f64..73898cd 100644 --- a/api/index.ts +++ b/api/index.ts @@ -4,7 +4,9 @@ import { ValidationPipe } from '@nestjs/common'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import express from 'express'; import bodyParser from 'body-parser'; -import { join } from 'path'; + +// 从源码导入,TypeScript 会编译到 dist +import { AppModule } from '../src/app.module'; // 缓存 app 实例,避免每次冷启动都重新创建 let cachedApp: any; @@ -13,19 +15,6 @@ async function bootstrap() { if (!cachedApp) { const expressApp = express(); - // 动态导入模块,支持构建后的路径 - 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), @@ -34,11 +23,7 @@ async function bootstrap() { // 配置 CORS app.enableCors({ - origin: [ - 'https://tradingadvfrontend.vercel.app', - 'http://localhost:8000', - 'http://localhost:3030', - ], + origin: true, // 允许所有来源 credentials: true, methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'], allowedHeaders: [ @@ -51,11 +36,10 @@ async function bootstrap() { ], }); - // 尝试加载全局过滤器和拦截器(如果存在) + // 尝试加载全局过滤器和拦截器 try { - 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')); + const { HttpExceptionFilter } = await import('../src/common/filters/http-exception.filter'); + const { TransformInterceptor } = await import('../src/common/interceptors/transform.interceptor'); app.useGlobalFilters(new HttpExceptionFilter()); app.useGlobalInterceptors(new TransformInterceptor()); } catch (error) { diff --git a/vercel.json b/vercel.json index de27626..c41475d 100644 --- a/vercel.json +++ b/vercel.json @@ -2,15 +2,14 @@ "version": 2, "builds": [ { - "src": "api/index.ts", + "src": "package.json", "use": "@vercel/node" } ], "routes": [ { "src": "/(.*)", - "dest": "/api/index.ts" + "dest": "/dist/api/index.js" } - ], - "installCommand": "pnpm install && pnpm run build" + ] }