使用 Nestjs + Azure 函数配置 swagger
Configure swagger with Nestjs + Azure functions
我正在尝试使用这篇文章之后的 azure 函数开发我的 nestjs:
https://trilon.io/blog/deploy-nestjs-azure-functions
我在我的应用程序中配置了 Swagger,如下所示:
...
const options = new DocumentBuilder()
.setTitle('App title')
.setDescription('App description')
.setVersion('1.0')
.addBearerAuth(
{
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
'authorization',
)
.addTag('freight')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('swagger', app, document);
...
当我 运行 开发中的应用程序时,我可以通过导航到 /swagger
来访问我的 swagger UI,但是当我 运行 npm run build && func host start
,我收到 500
错误,当我到达一条不存在的路线时也会发生这种情况。
在应用程序中注册的所有其他路由都按预期工作。
Nestjs 有一个模块需要为此加载。 nestjs-openapi
接下来需要指定几个配置项。
注意:Main.ts根本没有用到。
同步端口:
func host start --port 3000
!!使用 main.azure.ts 内的应用程序实例。示例假设在下面的代码上方定义了全局前缀。
...
//config
const config = new DocumentBuilder()
.setTitle('My Title')
.setDescription('My Description')
.setVersion('1.0')
.setBasePath('api-docs')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document, {
useGlobalPrefix: true,
});
// order matters here.
app.init()
// port that is used for swagger ui. Sync with Az Fx.
app.listen(3000)
现在你可以路由到 localhost:3000/{global_prefix}/api-docs 来加载 swagger ui.
我正在尝试使用这篇文章之后的 azure 函数开发我的 nestjs: https://trilon.io/blog/deploy-nestjs-azure-functions
我在我的应用程序中配置了 Swagger,如下所示:
...
const options = new DocumentBuilder()
.setTitle('App title')
.setDescription('App description')
.setVersion('1.0')
.addBearerAuth(
{
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
'authorization',
)
.addTag('freight')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('swagger', app, document);
...
当我 运行 开发中的应用程序时,我可以通过导航到 /swagger
来访问我的 swagger UI,但是当我 运行 npm run build && func host start
,我收到 500
错误,当我到达一条不存在的路线时也会发生这种情况。
在应用程序中注册的所有其他路由都按预期工作。
Nestjs 有一个模块需要为此加载。 nestjs-openapi
接下来需要指定几个配置项。
注意:Main.ts根本没有用到。
同步端口:
func host start --port 3000
!!使用 main.azure.ts 内的应用程序实例。示例假设在下面的代码上方定义了全局前缀。
...
//config
const config = new DocumentBuilder()
.setTitle('My Title')
.setDescription('My Description')
.setVersion('1.0')
.setBasePath('api-docs')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document, {
useGlobalPrefix: true,
});
// order matters here.
app.init()
// port that is used for swagger ui. Sync with Az Fx.
app.listen(3000)
现在你可以路由到 localhost:3000/{global_prefix}/api-docs 来加载 swagger ui.