我正在使用swagger-express-mw NPM包来创建REST服务,当我使用“swagger project start”运行项目然后它通过HTTP发布API时,我如何使用HTTPS代替。
我使用了使用vanilla npm软件包的HTTPS,如下所示:
var fs = require('fs');
var https = require('https');
var app = require('express')();
var options = {
key : fs.readFileSync('my.private.key'),
cert : fs.readFileSync('my.certificate.cer')
};
app.get('/', function (req, res) {
res.send('Yuhooo! Response over HTTPS!!! ');
});
https.createServer(options, app).listen(8443, function () {
console.log('Server started @ 8443!');
});
但是我不知道如何用swagger-express-mw实现相同的功能,下面是我的app.js启动监听器的代码片段。没有任何选项可以使用HTTPS作为协议
SwaggerExpress.create(configuration, function(err, swaggerExpress) {
if (err) { throw err; }
// install middleware
swaggerExpress.register(app);
var port = config.get('server.port') || process.env.PORT || 8080;
app.listen(port);
console.log('Server started at port %d', port);
});
var swaggerDoc = jsYaml.load(fs.readFileSync('./api/swagger/swagger.yaml'));
// Initialize the Swagger middleware for the api doc purpose
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
});
答案 0 :(得分:3)
app.listen
只是您可以使用的快捷方式
SwaggerExpress.create(configuration, function(err, swaggerExpress) {
if (err) { throw err; }
// install middleware
swaggerExpress.register(app);
var port = process.env.PORT || 443;
https.createServer(options, app).listen(port, function () {
console.log('Server started @ %s!', port);
});
});
答案 1 :(得分:0)
将Swagger 2.0规范与中间件一起使用,您的swagger配置文件可以设置为仅接受某些方案:
# Schemes is statically set here but will be overridden in app.js with
swagger object
schemes:
- https
您可以查看规范并转到固定字段:http://swagger.io/specification/