javascript - Swagger UI 总是出现在 NestJS 中
问题描述
我目前正在学习 NestJS。现在我正在尝试使用 Swagger 功能。我按照 NestJS 网站上的说明进行操作,并显示了一个不错的 Swagger 页面。但是,现在我不再能够使用我的控制器路径。
示例:
我有一个/api/users
将返回用户记录列表的路径。添加 Swagger 功能后,我打开了 Swagger UI /api
。当我尝试请求时,/api/users
我也得到了招摇的 UI,这次是空的。
当我单击“用户”API 的“试用”按钮/users
而不是/api/users
将执行时,当然会出现 404 响应。
我究竟做错了什么?请帮忙。
解决方案
我假设您已将应用程序的全局前缀设置为/api
. 然后,您还必须相应地设置 swagger 的基本路径。此外,您应该将 swagger 文档安装到未使用的路径:
// Set the global prefix for all controllers to /api
app.setGlobalPrefix('api');
const document = SwaggerModule.createDocument(
app,
new DocumentBuilder()
// Set the base path for swagger accordingly
.setBasePath('api')
.build(),
);
// Choose an unused path to mount your swagger module
SwaggerModule.setup('docs', app, document);
// ^^^^^^
推荐阅读
- kotlin - 如何根据动态创建的谓词集合过滤对象列表
- python - How to fill a circle with a gradient?
- ruby-on-rails - 我正在编写 Micheal Hartls Rail Tutorial 这本书我使用了相同的代码但得到了不同的结果
- ruby-on-rails - 如何在日志记录中过滤部分路径(不是参数)?
- flutter - 如何在 Flutter Web 中的 Row 中调整 IconButton 的大小?图标大小呈现在行视图之外
- laravel - 未找到 Laravel 5.8 类“Doctrine\DBAL\Driver\PDOMySql\Driver”
- python - 我需要我的代码只接受浮点值
- mysql - 连接到同一 DBMS 上的“外部”数据库
- java - 使用 Spring Boot 解析未转义的 Json
- javascript - 使用基本身份验证获取对 localhost 的请求