我用集成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文件了。这个问题有什么解决办法吗?
发布于 2019-11-25 09:26:04
这是因为你在听
app.use('/api-docs',swaggerUi.serve,swaggerUi.setup(swaggerDoc))
并试图访问http://localhost:8080/docs。在将上面的行更改为:app.use('/docs',swaggerUi.serve,swaggerUi.setup(swaggerDoc))后,您能试一试吗?
https://stackoverflow.com/questions/59027461
复制相似问题