Swagger for Nestjs with Azure functions -不工作

cig3rfwq  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(63)

我们正在使用@nestjs/azure-func-http使用nestjs开发Azure函数
https://trilon.io/blog/deploy-nestjs-azure-functions
swagger没有为函数应用加载。它只适用于Nest应用程序级别。
func host start -build --port 3001 does not load swagger at http://localhost:3001/swagger.
代码写在main. azure. ts上。

export async function createApp(): Promise\<INestApplication\> {

    const app = await NestFactory.create(AppModule);
    const options = new DocumentBuilder().build();
    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup('swagger', app, document);
    app.setGlobalPrefix('api');
    await app.init();
    await app.listen(3001); // default azure func port
    return app;

}
export { AppModule };

部署到Azure后,它需要工作。
缺少的是什么。需要解决方案。请帮

jv4diomz

jv4diomz1#

参考Document(或)按照以下步骤使用Swagger for NestJS Azure函数:

  • 我已经创建了一个默认的NestJs Azure函数,使用以下命令:
1. npm new <project_name>
2. cd <project_name>
3. npm add @azure/functions
4. npm add @schematics/angular
5. nest add @nestjs/azure-func-http

*要使用Swagger,需要加载Nestjs OpenAI:

  • 安装Swagger模块:

npm install --保存@nestjs/swagger

  • src/main.ts函数代码中导入Swagger模块
import { SwaggerModule, DocumentBuilder } from  '@nestjs/swagger';

我的函数代码:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const config = new DocumentBuilder()
    .setTitle('Swagger example')
    .setDescription('The Open API description')
    .setVersion('1.0')
    .addTag('Swagger')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document);

  await app.listen(3000);
}
bootstrap();

*打开终端=>运行npm run start

*浏览器点击https://localhost:3000/api

参考文献:

相关问题