简体   繁体   中英

Swagger UI Express - Cannot GET /docs/

I implemented a simple server with integrating Swagger documentation. My simple code as following

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);
});

After executing node server.js , I access http://localhost:8080/docs and I got

Cannot GET /docs/

Of course I already have swagger.yaml file inside /api . Any solution to this problem, please?

This is happening because you are listening on

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

and trying to access http://localhost:8080/docs . Can you try after changing the above line to: app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM