【发布时间】:2016-07-18 16:53:04
【问题描述】:
我使用 swagger project create 为节点服务建立 API。我关注了these instructions。我也在使用 swagger-tools 来服务 swagger-ui。
我想将 API 的 basePath 及其文档从“/”和“/docs”动态更改为 routingPath 和 routingPath +“/docs”。
我能够更改我的 swagger API 规范的 basePath,但我不确定如何更改 swagger-ui 的 basePath。我的代码如下所示:
'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'));
}
});
如果我运行此服务,我会在以下位置找到我的 API 规范 http://localhost:3000/api/collection
我的文档页面位于 http://localhost:3000/docs
我想在以下位置提供文档页面 http://localhost:3000/api/collection/docs
我试图通过将 basePath 选项传递给 swaggerUi 构造函数来做到这一点。
// enable SwaggerUI
app.use(swaggerExpress.runner.swaggerTools.swaggerUi({
basePath: routingPath // this has no effect
}));
那没用。有人知道如何配置 swagger-ui 的 basePath 吗?
【问题讨论】:
标签: swagger swagger-ui