Ich habe verwendet Swagger-Projekt erstellen, um eine API für einen Knoten-Service zu stehen. Ich folgte these instructions. Ich benutze auch Swagger-Tools, um den Swagger-Ui zu bedienen.Change basePath für swagger-u
Ich möchte den BasePath der API und ihre Dokumentation dynamisch von '/' und '/ Docs' zu routingPath und routingPath + '/ docs' ändern.
Ich bin in der Lage, die BasePath meiner Swagger API-Spezifikation zu ändern, aber ich bin mir nicht sicher, wie Sie den BasePath der Swagger-Ui ändern. Mein Code sieht wie folgt aus:
'use strict';
const defaultRoutingPath = '/api/collection';
const defaultPort = 3000;
var path = require('path');
var SwaggerExpress = require('swagger-express-mw');
var SwaggerUi = require('swagger-tools/middleware/swagger-ui');
var app = require('express')();
module.exports = app; // for testing
var routingPath = process.env.ROUTING_PATH || defaultRoutingPath;
var config = {
appRoot: __dirname // required config
};
SwaggerExpress.create(config, function(err, swaggerExpress) {
if (err) { throw err; }
swaggerExpress.runner.swagger.basePath = routingPath; // this works
app.get('/', (req, res) => {
res.redirect(path.join(routingPath, 'docs'));
})
// enable SwaggerUI
app.use(swaggerExpress.runner.swaggerTools.swaggerUi({
basePath: routingPath // this has no effect
}));
// install middleware
swaggerExpress.register(app);
var port = process.env.PORT || defaultPort;
app.listen(port);
if (swaggerExpress.runner.swagger.paths['/hello']) {
console.log('try this:\ncurl http://127.0.0.1:' + port + path.join(routingPath, '/hello?name=Scott'));
}
});
Wenn ich diesen Dienst ausgeführt wird, werde ich meine API-Spezifikation bei http://localhost:3000/api/collection
Und meine docs Seite bei http://localhost:3000/docs
ich die Dokumentation dienen möchte finden Seite um http://localhost:3000/api/collection/docs
Ich habe versucht, das zu tun, indem Sie eine BasePath-Option an den SwaggerUi-Konstruktor übergeben.
// enable SwaggerUI
app.use(swaggerExpress.runner.swaggerTools.swaggerUi({
basePath: routingPath // this has no effect
}));
Das hat nicht funktioniert. Wer weiß, wie man den basePath des swagger-ui konfiguriert?