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" + ] }