使用NodeJS swagger-express-mw npm包的HTTPS

时间:2016-07-27 09:05:12

标签: node.js express https swagger swagger-2.0

我正在使用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());
});

2 个答案:

答案 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/