fix: 修复 Vercel 部署 404 和 Swagger 无法访问
- vercel.json: builds.src 从 package.json 改为 api/index.ts,routes.dest 从 /dist/api/index.js 改为 /api - api/index.ts: 修正 require 路径,移除多余的 src/ 前缀 - api/index.ts: Swagger 挂载路径从 /api 改为 /docs,避免与 Vercel 函数路径冲突 - package.json: 添加 vercel-build 脚本确保部署前编译 NestJS
This commit is contained in:
parent
f557f8581b
commit
914e156cc6
16
api/index.ts
16
api/index.ts
|
|
@ -13,12 +13,12 @@ async function bootstrap() {
|
|||
const expressApp = express();
|
||||
|
||||
// 使用 require 动态加载编译后的模块(避免 TypeScript 编译时检查)
|
||||
const { AppModule } = require('../dist/src/app.module');
|
||||
const { AppModule } = require('../dist/app.module');
|
||||
|
||||
const app = await NestFactory.create(
|
||||
AppModule,
|
||||
new ExpressAdapter(expressApp),
|
||||
{ logger: ['error', 'warn', 'log'] }
|
||||
{ logger: ['error', 'warn', 'log'] },
|
||||
);
|
||||
|
||||
// 配置 CORS
|
||||
|
|
@ -38,8 +38,12 @@ async function bootstrap() {
|
|||
|
||||
// 尝试加载全局过滤器和拦截器
|
||||
try {
|
||||
const { HttpExceptionFilter } = require('../dist/src/common/filters/http-exception.filter');
|
||||
const { TransformInterceptor } = require('../dist/src/common/interceptors/transform.interceptor');
|
||||
const {
|
||||
HttpExceptionFilter,
|
||||
} = require('../dist/common/filters/http-exception.filter');
|
||||
const {
|
||||
TransformInterceptor,
|
||||
} = require('../dist/common/interceptors/transform.interceptor');
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
app.useGlobalInterceptors(new TransformInterceptor());
|
||||
} catch (error) {
|
||||
|
|
@ -67,7 +71,7 @@ async function bootstrap() {
|
|||
.addBearerAuth()
|
||||
.build();
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('api', app, document);
|
||||
SwaggerModule.setup('docs', app, document);
|
||||
|
||||
await app.init();
|
||||
cachedApp = expressApp;
|
||||
|
|
@ -88,7 +92,7 @@ export default async function handler(req: any, res: any) {
|
|||
res.status(500).json({
|
||||
error: 'Internal Server Error',
|
||||
message: error.message,
|
||||
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
|
||||
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@
|
|||
"test:watch": "jest --watch",
|
||||
"test:cov": "jest --coverage",
|
||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json",
|
||||
"vercel-build": "npx nest build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/cache-manager": "2.1.0",
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@
|
|||
"version": 2,
|
||||
"builds": [
|
||||
{
|
||||
"src": "package.json",
|
||||
"src": "api/index.ts",
|
||||
"use": "@vercel/node"
|
||||
}
|
||||
],
|
||||
"routes": [
|
||||
{
|
||||
"src": "/(.*)",
|
||||
"dest": "/dist/api/index.js"
|
||||
"dest": "/api"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue