Compare commits
No commits in common. "b91a17b07137a6d67a355faa2273fc89cf3da1fe" and "1d8213f6ed508c4ff0ee0295a248a9704a0a6eb8" have entirely different histories.
b91a17b071
...
1d8213f6ed
39
api/index.ts
39
api/index.ts
|
|
@ -1,31 +1,19 @@
|
|||
import { NestFactory } from '@nestjs/core';
|
||||
import { ExpressAdapter } from '@nestjs/platform-express';
|
||||
import { AppModule } from '../dist/app.module';
|
||||
import { AppModule } from '../src/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';
|
||||
import * as bodyParser from 'body-parser';
|
||||
|
||||
// 缓存 app 实例,避免每次冷启动都重新创建
|
||||
let cachedApp: any;
|
||||
let cachedApp;
|
||||
|
||||
async function bootstrap() {
|
||||
if (!cachedApp) {
|
||||
const expressApp = express();
|
||||
const app = await NestFactory.create(
|
||||
AppModule,
|
||||
new ExpressAdapter(expressApp),
|
||||
);
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
// 配置 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: [
|
||||
|
|
@ -38,11 +26,8 @@ async function bootstrap() {
|
|||
],
|
||||
});
|
||||
|
||||
// 全局过滤器和拦截器
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
app.useGlobalInterceptors(new TransformInterceptor());
|
||||
|
||||
// 全局验证管道
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
|
|
@ -51,13 +36,12 @@ async function bootstrap() {
|
|||
}),
|
||||
);
|
||||
|
||||
// Body parser 配置
|
||||
app.use(bodyParser.json({ limit: '50mb' }));
|
||||
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
|
||||
|
||||
// Swagger 文档
|
||||
// Swagger 配置
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('API 文档')
|
||||
.setTitle('我的应用 API')
|
||||
.setDescription('这是我的应用程序的 API 文档')
|
||||
.setVersion('1.0')
|
||||
.addBearerAuth()
|
||||
|
|
@ -66,13 +50,14 @@ async function bootstrap() {
|
|||
SwaggerModule.setup('api', app, document);
|
||||
|
||||
await app.init();
|
||||
cachedApp = expressApp;
|
||||
cachedApp = app;
|
||||
}
|
||||
|
||||
return cachedApp;
|
||||
}
|
||||
|
||||
// Vercel Serverless handler
|
||||
export default async function handler(req: any, res: any) {
|
||||
export default async (req, res) => {
|
||||
const app = await bootstrap();
|
||||
app(req, res);
|
||||
}
|
||||
const server = app.getHttpAdapter().getInstance();
|
||||
return server(req, res);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue