oauth_nest_demo/api/index.ts

111 lines
3.3 KiB
TypeScript

import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
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;
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),
{ logger: ['error', 'warn', 'log'] }
);
// 配置 CORS
app.enableCors({
origin: [
'https://tradingadvfrontend.vercel.app',
'http://localhost:8000',
'http://localhost:3030',
],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: [
'Content-Type',
'Authorization',
'Accept',
'Origin',
'Referer',
'User-Agent',
],
});
// 尝试加载全局过滤器和拦截器(如果存在)
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'));
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalInterceptors(new TransformInterceptor());
} catch (error) {
console.log('Global filters/interceptors not loaded:', error.message);
}
// 全局验证管道
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
// Body parser 配置
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
// Swagger 文档
const config = new DocumentBuilder()
.setTitle('API 文档')
.setDescription('这是我的应用程序的 API 文档')
.setVersion('1.0')
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.init();
cachedApp = expressApp;
console.log('NestJS app initialized successfully');
}
return cachedApp;
}
// 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,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
}