-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
136 lines (120 loc) · 3.83 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*jslint node: true */
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
var compress = require('compression');
var fs = require('fs');
var path = require('path');
var config = require('./config');
var serveStatic = require('serve-static');
var helpers = require('./utils/helpers');
var logger = require("./utils/logger");
///Required to initialize all the models. Add new models here.
var CustomError = require("./models/common/errors/custom-error");
var errorCodes = require("./models/common/errors/error-codes");
var commonMiddleware = require('./middlewares/common');
var expressWinston = require('express-winston');
var winston = require('winston');
require('dotenv').config();
if (config.debug) {
app.use(compress());
}
app.disable('etag');
app.disable('x-powered-by');
app.all('/*', commonMiddleware.corsMiddleWare);
app.use(serveStatic(__dirname + '/public', {
maxAge: '5d',
setHeaders: setCustomCacheControl
}));
function setCustomCacheControl(res, path) {
if (serveStatic.mime.lookup(path) === 'text/html') {
// Custom Cache-Control for HTML files
res.setHeader('Cache-Control', 'public, max-age=0');
}
}
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
var listeningPort = process.env.PORT || 9099;
var responseMaker = require("./utils/response-maker");
var apiRoutes = express.Router();
app.use(function (err, req, res, next) {
return responseMaker.prepareResponse(err, null, res);
});
apiRoutes.get('/', function (req, res) {
res.redirect('/index.html');
});
apiRoutes.get('/error', function (req, res, next) {
// here we cause an error in the pipeline so we see express-winston in action.
return next(new Error("This is an error and it should be logged to the console"));
});
var queryRoute = require('./routes/v1/json_query_route')();
app.use('/', function (req, res, next) {
next();
});
if (config.debug) {
app.use(expressWinston.logger({
transports: [
new winston.transports.File({
level: 'info',
filename: './logs/logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: true,
colorize: true
})
]
}));
}
app.use('/api/v1/query', queryRoute);
process.on("unhandledRejection", function (promise, reason) {
logger.error("Error occurred", promise, reason);
});
process.on('uncaughtException', function (err) {
logger.error(err);
throw err;
});
if (config.debug) {
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.File({
level: 'info',
filename: './logs/logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: true,
colorize: true
})
]
}));
}
app.use(function (req, res, next) {
var err = new CustomError('End point not found.', {
errorCode: errorCodes.NOT_FOUND
});
next(err);
});
app.use(function (err, req, res, next) {
err.isNotReThrow = true;
err.url = helpers.getUrl(req);
return responseMaker.prepareResponse(err, null, res);
});
var http = require('http').Server(app);
var server = http.listen(listeningPort, function () {
var port = server.address().port;
});