gpt4 book ai didi

azure - 使用 Nestjs + Azure 函数配置 swagger

转载 作者:行者123 更新时间:2023-12-02 23:00:18 25 4
gpt4 key购买 nike

我正在尝试使用本文中的 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 + Azure 函数配置 swagger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61952027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com