diff --git a/api/index.ts b/api/index.ts index c0659b4..70222f9 100644 --- a/api/index.ts +++ b/api/index.ts @@ -1,8 +1,5 @@ import { NestFactory } from '@nestjs/core'; import { ExpressAdapter } from '@nestjs/platform-express'; -import { AppModule } from '../dist/app.module'; -import { HttpExceptionFilter } from '../src/common/filters/http-exception.filter'; -import { TransformInterceptor } from '../src/common/interceptors/transform.interceptor'; import { ValidationPipe } from '@nestjs/common'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import * as express from 'express'; @@ -14,6 +11,10 @@ let cachedApp: any; async function bootstrap() { if (!cachedApp) { const expressApp = express(); + + // 动态导入模块,支持构建后的路径 + const { AppModule } = await import('../dist/app.module.js'); + const app = await NestFactory.create( AppModule, new ExpressAdapter(expressApp), @@ -38,9 +39,15 @@ async function bootstrap() { ], }); - // 全局过滤器和拦截器 - app.useGlobalFilters(new HttpExceptionFilter()); - app.useGlobalInterceptors(new TransformInterceptor()); + // 尝试加载全局过滤器和拦截器(如果存在) + try { + const { HttpExceptionFilter } = await import('../dist/common/filters/http-exception.filter.js'); + const { TransformInterceptor } = await import('../dist/common/interceptors/transform.interceptor.js'); + app.useGlobalFilters(new HttpExceptionFilter()); + app.useGlobalInterceptors(new TransformInterceptor()); + } catch (error) { + console.log('Global filters/interceptors not loaded:', error.message); + } // 全局验证管道 app.useGlobalPipes( @@ -73,6 +80,14 @@ async function bootstrap() { // Vercel Serverless handler export default async function handler(req: any, res: any) { - const app = await bootstrap(); - app(req, res); + try { + 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 + }); + } } diff --git a/package.json b/package.json index 15023c4..b0cc637 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "@nestjs/platform-express": "^10.0.0", "@nestjs/swagger": "^11.0.2", "@nestjs/typeorm": "^10.0.2", + "express": "^4.18.2", "@types/bcrypt": "^5.0.2", "@types/passport-github2": "^1.2.9", "@types/uuid": "^10.0.0", diff --git a/vercel.json b/vercel.json index ca7e533..52d7759 100644 --- a/vercel.json +++ b/vercel.json @@ -2,19 +2,14 @@ "version": 2, "builds": [ { - "src": "api/index.ts", + "src": "package.json", "use": "@vercel/node" } ], "routes": [ { "src": "/(.*)", - "dest": "api/index.ts" + "dest": "/api/index.ts" } - ], - "buildCommand": "npx nest build", - "outputDirectory": "dist", - "env": { - "NODE_ENV": "production" - } + ] }