swagger-node icon indicating copy to clipboard operation
swagger-node copied to clipboard

Enable Swagger-UI

Open ncipollina opened this issue 7 years ago • 6 comments

If I follow the getting started instructions for this project, how do I enable swagger-ui for a node express API?

ncipollina avatar Oct 02 '17 13:10 ncipollina

I ended up using the docker-container:

docker run -p 8080:8080 -e API_URL=http://localhost:10010/your-basePath-here/swagger swaggerapi/swagger-ui

Note: Swagger-ui does make a CORS-Request to call the swagger endpoint, so you need to add the following lines to your app.js:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://localhost:8080");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

Then serve to http://localhost:8080

Hope, that helps.

MrApe avatar Oct 13 '17 15:10 MrApe

Having created the initial project by swagger project create it worked like this for me (with Express):

'use strict';

<...>
var SwaggerUi = require('swagger-tools/middleware/swagger-ui');

var app = express();

<...>

SwaggerExpress.create(config, function(err, swaggerExpress) {
  if (err) { throw err; }

  // install middleware
  app.use(SwaggerUi(swaggerExpress.runner.swagger));

  swaggerExpress.register(app);

  var port = process.env.PORT || 10010;
  app.listen(port);

});

<...>

By default the Swagger is then available at /docs (if your basePath is /).

arne-at-daten-und-bass avatar Oct 20 '17 10:10 arne-at-daten-und-bass

@arne-at-daten-und-bass thank you for your comment! I really hope this gets a full implementation soon, it's the only thing keeping this module from being amazing!

SpencerKaiser avatar Nov 14 '17 06:11 SpencerKaiser

How can I change the base path? I have the raw swagger here bddd.com/seach-api/swagger and want to change the /docs to bddd.com/seach-api/docs

Tim-Schwalbe avatar May 04 '18 16:05 Tim-Schwalbe

Did you set the basePath to /seach-api in your swagger.yaml?

https://swagger.io/docs/specification/2-0/api-host-and-base-path/

arne-at-daten-und-bass avatar May 04 '18 17:05 arne-at-daten-und-bass

https://github.com/swagger-api/swagger-node/issues/524#issuecomment-338164612

No longer works. I had to add swagger-tools

var SwaggerUi = require('swagger-tools/middleware/swagger-ui');

 SwaggerExpress.create(config, function(err, swaggerExpress) {
    if (err) { throw err; }

    // Add swagger-ui (This must be before swaggerExpress.register)
    webserver.use(SwaggerUi(swaggerExpress.runner.swagger));

    // install middleware
    swaggerExpress.register(webserver);
    ...
  });

shishirsharma avatar Jun 19 '18 06:06 shishirsharma