-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
67 lines (56 loc) · 1.89 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
const https = require('https');
const util = require('util');
const bodyParser = require('body-parser');
const cors = require('cors');
const SwaggerExpress = require('swagger-express-mw');
const express = require('express');
const swaggerUi = require('swagger-tools/middleware/swagger-ui');
const swaggerMetadata = require('swagger-tools/middleware/swagger-metadata');
const config = require('./config/config');
const middleware = require('./api/middleware/middleware');
const logger = require('./api/helpers/logger');
if (config.NODE_ENV === 'development' ||
config.NODE_ENV === 'test') {
require('pretty-error').start();
}
const db = require('./config/db');
const app = express();
// will support such content type application/json at same time
app.use(bodyParser.json({
type: ['application/json']
}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors({ maxAge: 600 }));
app.use(middleware.bindDb(app, db));
app.use(middleware.logging);
app.use(middleware.camelCaseBody);
app.set('x-powered-by', false);
var seConfig = { appRoot: __dirname };
let sslConfig = null;
if (config.USE_SSL) {
sslConfig = {
key: config.SSL_PRIV_KEY,
cert: config.SSL_CERT,
ca: config.SSL_CA
};
}
SwaggerExpress.create(seConfig, function(err, swaggerExpress) {
if (err) { throw err; }
app.use(swaggerMetadata(swaggerExpress.runner.swagger))
app.use(swaggerUi(swaggerExpress.runner.swagger));
// install middleware
swaggerExpress.register(app);
if (config.USE_SSL) {
https.createServer(sslConfig, app).listen(config.SSL_PORT, (err) => {
if (err) { logger.error(err); }
else { logger.info(util.format('Express running on port %s', config.SSL_PORT)); }
});
} else {
app.listen(config.PORT, (err) => {
if (err) { logger.error(err); }
else { logger.info(util.format('Express running on port %s', config.PORT)); }
});
}
});
module.exports = app;