-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
52 lines (44 loc) · 1.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
var SwaggerExpress = require('swagger-express-mw');
var SwaggerUi = require('swagger-ui-express');
var app = require('express')();
module.exports = app; // for testing
var config = {
appRoot: __dirname // required config
};
SwaggerExpress.create(config, function(err, swaggerExpress) {
if (err) {
throw err;
}
// install middleware
swaggerExpress.register(app);
// add swagger ui
var swaggerDocument = swaggerExpress.runner.swagger;
var options = {
docExpansion: "full",
tryItOutEnabled: true,
syntaxHighlight: false,
url: "https://" + swaggerDocument.host + "/swagger"
};
var customCss = '#header { display: none }';
var port = process.env.PORT || 10010;
app.use('/docs', SwaggerUi.serve, function (req, res) {
// override config for localhost
if (req.get('Host').includes('localhost')) {
swaggerDocument.host = `localhost:${port}`;
swaggerDocument.schemes = ['http'];
options.url = `http://localhost:${port}/swagger`;
}
var handler = SwaggerUi.setup(swaggerDocument, false, options, customCss);
handler(req, res);
});
//redirect root to /docs
app.get('/', function (req, res) {
res.redirect('/docs')
})
//this is needed to avoid EADDRINUSE errors
//see http://www.marcusoft.net/2015/10/eaddrinuse-when-watching-tests-with-mocha-and-supertest.html
if(!module.parent) {
app.listen(port);
}
});