简体   繁体   English

使用 Nestjs + Azure 函数配置 swagger

[英]Configure swagger with Nestjs + Azure functions

I am trying to develop my nestjs using azure functions following this article: https://trilon.io/blog/deploy-nestjs-azure-functions我正在尝试使用本文后面的 azure 函数开发我的 nestjs: https://trilon.io/blog/deploy-nestjs-azure-functions

I have configured Swagger in my application as follows:我在我的应用程序中配置了 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);
...

When I run the app in development, I can access my swagger UI by navigating to /swagger , however when I run npm run build && func host start , I receive 500 error, which also happens when I hit a non-existing route.当我在开发中运行该应用程序时,我可以通过导航到/swagger来访问我的 swagger UI,但是当我运行npm run build && func host start ,我收到500错误,当我点击一条不存在的路线时也会发生这种情况。

All other routes that are registered in the application work as expected.在应用程序中注册的所有其他路由都按预期工作。

Nestjs has a module you need to load for this. Nestjs 有一个你需要为此加载的模块。 nestjs-openapi nestjs-openapi

Next you will need to specify a few configuration items.接下来您需要指定一些配置项。

Note: Main.ts is not used at all.注意:根本不使用 Main.ts。

Sync ports with:同步端口:

func host start --port 3000

.. Use the app instance within the main.azure.ts. .. 在 main.azure.ts 中使用应用程序实例。 Example assumes global prefix defined above the code below.示例假定在下面的代码上方定义了全局前缀。

...
//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)

Now you can route to localhost:3000/{global_prefix}/api-docs to load swagger ui.现在你可以路由到 localhost:3000/{global_prefix}/api-docs 来加载 swagger ui。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM