swagger-node
swagger-node copied to clipboard
Enable Swagger-UI
If I follow the getting started instructions for this project, how do I enable swagger-ui for a node express API?
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.
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 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!
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
Did you set the basePath
to /seach-api
in your swagger.yaml
?
https://swagger.io/docs/specification/2-0/api-host-and-base-path/
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);
...
});