我无法创建一个端点来提供大张旗鼓的文档。我可以在本地执行此操作的唯一方法是路径末尾是否有双斜杠localhost:3003/dev/swagger//
;如果我省略了一个正斜杠,它会为localhost:3003/swagger
没有/dev
. 部署后,API Gateway 将返回 {"message": "Forbidden"}
任一 swagger 端点(带或不带//
)。如何让 API Gateway/swagger
端点返回 swagger UI?,我不确定我是否错过了一些步骤。
下面是我的 NestJS 应用程序的 main.ts 以及 serverless.yml,这里是一个示例 repo,它具有最少的设置来复制我的问题。https://github.com/MRdgz/serverless-nestj-swagger
main.ts
// main.ts
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { configure as serverlessExpress } from '@vendia/serverless-express';
import { Callback, Context, Handler } from 'aws-lambda';
import { AppModule } from './app.module';
let server: Handler;
function setupSwagger(nestApp: INestApplication): void {
const config = new DocumentBuilder()
.setTitle('Sample API')
.setDescription('Sample API Documentation')
.setVersion('0.0.1')
.addServer('/dev')
.build();
const document = SwaggerModule.createDocument(nestApp, config);
SwaggerModule.setup('/swagger', nestApp, document, {
customSiteTitle: 'Sample',
swaggerOptions: {
docExpansion: 'none',
operationSorter: 'alpha',
tagSorter: 'alpha',
},
});
}
async function bootstrap(): Promise<Handler> {
const app = await NestFactory.create(AppModule);
setupSwagger(app);
await app.init();
const expressApp = app.getHttpAdapter().getInstance();
return serverlessExpress({ app: expressApp });
}
export const handler: Handler = async (
event: any,
context: Context,
callback: Callback,
) => {
event.path = `${event.path}/`;
event.path = event.path.includes('swagger-ui')
? `swagger${event.path}`
: event.path;
server = server ?? (await bootstrap());
return server(event, context, callback);
};
无服务器.yml
service: sample-api
variablesResolutionMode: 20210326
useDotenv: true
plugins:
- serverless-offline
- serverless-plugin-optimize
# functions will inherit settings from provider properties if available,
provider:
name: aws
runtime: nodejs14.x
lambdaHashingVersion: 20201221
# memorySize: 1024 # default 1024 MB
timeout: 30 # default 6 seconds
# sls deploy --stage {stage} otherwise defaults to dev
stage: ${opt:stage, 'dev'}
functions:
main:
handler: dist/main.handler
name: ${opt:stage, 'dev'}-${self:service}
events:
- http:
method: ANY
path: /{proxy+}
cors: true
custom:
serverless-offline:
httpPort: 3003
optimize:
external: ['swagger-ui-dist']