Swagger ui 不加载函数http触发器和nestjs

问题描述 投票:0回答:1

我正在尝试为我的 azure http 触发功能配置 swagger,但是当我在浏览器中查询定义 swagger 的路径时,一些请求将处于待处理状态,如下图所示。我不知道会发生什么,我也无法解决它。如有任何帮助,我们将提前感激不尽。

enter image description here

仍待处理的请求是:

  • http://localhost:3000/api/project/swagger/swagger-ui.css
  • http://localhost:3000/api/project/swagger/swagger-ui-bundle.js
  • http://localhost:3000/api/project/swagger/swagger-ui-standalone-preset.js

我执行的运行该函数的命令是“npm run build && func start”。

这是我的应用程序设置的代码

import { DtoValidationError } from '@class/dto-validator-error.class';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { ValidationError } from 'class-validator';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { Context, HttpRequest } from '@azure/functions';
import { AzureHttpAdapter } from '@nestjs/azure-func-http';

export async function createApp(): Promise<INestApplication> {
    const app = await NestFactory.create(AppModule);
    app.setGlobalPrefix('api/project');

    const config = new DocumentBuilder()
      .setTitle('API')
      .setDescription('API PROJECT')
      .setVersion('1.0')
      .addServer('http://localhost:3000', 'Local Server')
      .build();

    const document = SwaggerModule.createDocument(app, config);
      SwaggerModule.setup('/swagger', app, document, {
          useGlobalPrefix: true
      });

    await app.init();
    return app;
}


export default function(context: Context, req: HttpRequest): void {
  AzureHttpAdapter.handle(createApp, context, req);
}
azure azure-functions nestjs swagger swagger-ui
1个回答
0
投票

我已按照以下步骤创建 NestJS Azure 函数并配置 Swagger:

npm i -g @nestjs/cli
nest new nsproj //Degrade reflect-metadata version to ^0.1.13
npm i @schematics/angular
nest add @nestjs/azure-func-http@latest
npm install --save @nestjs/swagger

更改package.json中的

Start
命令如下:

"start:azure": "npm run build && func host start --port 3000"

main.azure.ts:

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

export async function createApp(): Promise<INestApplication> {
    const app = await NestFactory.create(AppModule);
    app.setGlobalPrefix('api/project');

    const config = new DocumentBuilder()
      .setTitle('API')
      .setDescription('API PROJECT')
      .setVersion('1.0')
      .addTag('NestJs')
      .build();

    const document = SwaggerModule.createDocument(app, config);
      SwaggerModule.setup('swagger', app, document, {
          useGlobalPrefix: true
      });

    await app.init();
    await app.listen(3000);
    return app;
}

运行命令

npm run start:azure
来执行函数:

C:\Users\uname\nestfn\>npm run start:azure

> [email protected] start:azure
> npm run build && func host start --port 3000

> [email protected] build
> nest build

Azure Functions Core Tools
Core Tools Version:       4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875

Warning: Proxies are not supported in Azure Functions v4. Instead of 'proxies.json', try Azure API Management: https://aka.ms/AAfiueq
[2024-12-19T08:10:18.925Z] Worker process started and initialized.

Functions:

        main:  http://localhost:3000/api/{*segments}

For detailed output, run func with --verbose flag.
[2024-12-19T08:10:21.892Z] Executing 'Functions.main' (Reason='This function was programmatically called via the host APIs.', Id=9b8388a9-2374-4055-9e84-57cb10687964)
[2024-12-19T08:10:22.799Z] [Nest] 19508  - 12/19/2024, 1:40:22 PM     LOG [NestFactory] Starting Nest application...
[2024-12-19T08:10:22.821Z] [Nest] 19508  - 12/19/2024, 1:40:22 PM     LOG [InstanceLoader] AppModule dependencies initialized +23ms
[2024-12-19T08:10:22.849Z] [Nest] 19508  - 12/19/2024, 1:40:22 PM     LOG [RoutesResolver] AppController {/api}: +29ms
[2024-12-19T08:10:22.854Z] [Nest] 19508  - 12/19/2024, 1:40:22 PM     LOG [RouterExplorer] Mapped {/api, GET} route +4ms
[2024-12-19T08:10:22.858Z] [Nest] 19508  - 12/19/2024, 1:40:22 PM     LOG [NestApplication] Nest application successfully started +5ms
[2024-12-19T08:10:23.052Z] Executed 'Functions.main' (Succeeded, Id=9b8388a9-2374-4055-9e84-57cb10687964, Duration=1168ms)
[2024-12-19T08:10:23.147Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-12-19T08:10:23.168Z] Executing 'Functions.main' (Reason='This function was programmatically called via the host APIs.', Id=c38d6740-e076-48b3-a32e-f8b993f9dcbe)[2024-12-19T08:10:23.168Z] Executing 'Functions.main' (Reason='This function was programmatically called via the host APIs.', Id=f250abe7-ddf6-4f86-95b8-401f2acc8296)

[2024-12-19T08:10:23.174Z] Executing 'Functions.main' (Reason='This function was programmatically called via the host APIs.', Id=0d8b0320-88e4-44e6-97db-4e81fad274ce)
[2024-12-19T08:10:23.204Z] Executed 'Functions.main' (Succeeded, Id=f250abe7-ddf6-4f86-95b8-401f2acc8296, Duration=37ms)

路由至

localhost:3000/{global_prefix}/swagger/
以加载 Swagger UI。

http://localhost:3000/api/project/swagger/:

enter image description here

http://localhost:3000/api/project/swagger/swagger-ui.css:

enter image description here

http://localhost:3000/api/project/swagger/swagger-ui-bundle.js:

enter image description here

http://localhost:3000/api/project/swagger/swagger-ui-standalone-preset.js
: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.