merge: 合并 GitHub 远程更改并优化 serverless 配置
- 合并本地和远程的 api/index.ts 配置 - 使用 ExpressAdapter 提升 serverless 性能 - 保留全局过滤器和拦截器 - 保留 body-parser 大文件支持 - 统一 CORS 配置 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
commit
b91a17b071
39
api/index.ts
39
api/index.ts
|
|
@ -1,19 +1,31 @@
|
||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
import { AppModule } from '../src/app.module';
|
import { ExpressAdapter } from '@nestjs/platform-express';
|
||||||
|
import { AppModule } from '../dist/app.module';
|
||||||
import { HttpExceptionFilter } from '../src/common/filters/http-exception.filter';
|
import { HttpExceptionFilter } from '../src/common/filters/http-exception.filter';
|
||||||
import { TransformInterceptor } from '../src/common/interceptors/transform.interceptor';
|
import { TransformInterceptor } from '../src/common/interceptors/transform.interceptor';
|
||||||
import { ValidationPipe } from '@nestjs/common';
|
import { ValidationPipe } from '@nestjs/common';
|
||||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||||
|
import * as express from 'express';
|
||||||
import * as bodyParser from 'body-parser';
|
import * as bodyParser from 'body-parser';
|
||||||
|
|
||||||
let cachedApp;
|
// 缓存 app 实例,避免每次冷启动都重新创建
|
||||||
|
let cachedApp: any;
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
if (!cachedApp) {
|
if (!cachedApp) {
|
||||||
const app = await NestFactory.create(AppModule);
|
const expressApp = express();
|
||||||
|
const app = await NestFactory.create(
|
||||||
|
AppModule,
|
||||||
|
new ExpressAdapter(expressApp),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 配置 CORS
|
||||||
app.enableCors({
|
app.enableCors({
|
||||||
origin: true,
|
origin: [
|
||||||
|
'https://tradingadvfrontend.vercel.app',
|
||||||
|
'http://localhost:8000',
|
||||||
|
'http://localhost:3030',
|
||||||
|
],
|
||||||
credentials: true,
|
credentials: true,
|
||||||
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
||||||
allowedHeaders: [
|
allowedHeaders: [
|
||||||
|
|
@ -26,8 +38,11 @@ async function bootstrap() {
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 全局过滤器和拦截器
|
||||||
app.useGlobalFilters(new HttpExceptionFilter());
|
app.useGlobalFilters(new HttpExceptionFilter());
|
||||||
app.useGlobalInterceptors(new TransformInterceptor());
|
app.useGlobalInterceptors(new TransformInterceptor());
|
||||||
|
|
||||||
|
// 全局验证管道
|
||||||
app.useGlobalPipes(
|
app.useGlobalPipes(
|
||||||
new ValidationPipe({
|
new ValidationPipe({
|
||||||
whitelist: true,
|
whitelist: true,
|
||||||
|
|
@ -36,12 +51,13 @@ async function bootstrap() {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Body parser 配置
|
||||||
app.use(bodyParser.json({ limit: '50mb' }));
|
app.use(bodyParser.json({ limit: '50mb' }));
|
||||||
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
|
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
|
||||||
|
|
||||||
// Swagger 配置
|
// Swagger 文档
|
||||||
const config = new DocumentBuilder()
|
const config = new DocumentBuilder()
|
||||||
.setTitle('我的应用 API')
|
.setTitle('API 文档')
|
||||||
.setDescription('这是我的应用程序的 API 文档')
|
.setDescription('这是我的应用程序的 API 文档')
|
||||||
.setVersion('1.0')
|
.setVersion('1.0')
|
||||||
.addBearerAuth()
|
.addBearerAuth()
|
||||||
|
|
@ -50,14 +66,13 @@ async function bootstrap() {
|
||||||
SwaggerModule.setup('api', app, document);
|
SwaggerModule.setup('api', app, document);
|
||||||
|
|
||||||
await app.init();
|
await app.init();
|
||||||
cachedApp = app;
|
cachedApp = expressApp;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cachedApp;
|
return cachedApp;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (req, res) => {
|
// Vercel Serverless handler
|
||||||
|
export default async function handler(req: any, res: any) {
|
||||||
const app = await bootstrap();
|
const app = await bootstrap();
|
||||||
const server = app.getHttpAdapter().getInstance();
|
app(req, res);
|
||||||
return server(req, res);
|
}
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue