Swagger UI Express-无法获取/ docs /

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

我通过集成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文件。请解决这个问题吗?

node.js swagger-ui
1个回答
2
投票

发生这种情况是因为您正在听

app.use('/ api-docs',swaggerUi.serve,swaggerUi.setup(swaggerDoc))>] >>

并尝试访问http://localhost:8080/docs。将上面的行更改为:app.use('/ docs',swaggerUi.serve,swaggerUi.setup(swaggerDoc))

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