node.js - Swagger UI Express - 无法获取 /docs/
问题描述
我实现了一个带有集成 Swagger 文档的简单服务器。我的简单代码如下
const http = require('http');
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const bodyParser = require('body-parser');
const YAML = require('yamljs');
const serverPort = 8080;
const app = express();
const swaggerDoc = YAML.load('./api/swagger.yaml');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc));
app.use(bodyParser.urlencoded({
extended: false,
}));
app.use(bodyParser.json());
http.createServer(app).listen(serverPort, function () {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});
执行后node server.js
,我访问http://localhost:8080/docs我得到了
Cannot GET /docs/
当然我swagger.yaml
里面已经有文件了/api
。请问这个问题有什么解决办法吗?
解决方案
这是因为你在听
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc))
并尝试访问http://localhost:8080/docs。将上述行更改为: app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc))
推荐阅读
- php - 检查PHP中是否超过最大上传文件数
- swift - Swift在变量中获取坐标纬度经度
- azure-devops - VSTS 构建代理无法下载 VSTS 私有包
- symfony - 从扩展它的每个模板访问基本模板变量
- linux - 树莓派导入tensorflow分割错误
- swift - 如何在 Swift 中获取 MacOS 上打开的窗口列表?
- google-cloud-platform - GCP上的设计服务
- optimization - 如何在 Pytorch 中优化较低的 Cholesky 参数?
- jquery - HTML多选标签如何通过点击事件选择多个选项
- reactjs - 更改所选元素 onMouseEnter- React