This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (167 loc) · 4.75 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var local = require('./local');
var express = require('express'),
load = require('express-load'),
path = require('path'),
logger = require('morgan'),
cors = require('cors'),
bodyParser = require('body-parser'),
xmlparser = require('express-xml-bodyparser'),
i18n = require("./i18n"),
compression = require('compression');
var webpack = require("webpack");
var webpackConfig = require('./webpack.config');
var compiler = webpack(webpackConfig);
var session = require('express-session');
var log = require('./services/log.js');
require('babel-core/register')({
ignore: /node_modules\/(?!(react-slick|reflux-state-mixin)).*/
});
require('babel-polyfill');
var app = express();
//settings flags
app.enable('trust proxy');
app.disable('view cache'); //cache may be causing weird issues in production, due to our custom React view implementation
app.disable("x-powered-by");
process.on('uncaughtException', function(err) {
log.error('Caught exception: ' + err.stack);
});
if (app.get('env') !== 'production') {
require("nodejs-dashboard");
}
//use compression
app.use(compression());
//CORS
app.use(cors());
app.options('*', cors());
var sess = {
secret: local.SESSION_SECRET,
resave: false,
proxy: true,
saveUninitialized: true,
cookie: {
path: '/',
domain: '.' + local.host
}
};
if (app.get('env') === 'production') {
sess.cookie.secure = true;
}
app.use(session(sess));
//by default set language based on browser 'accept-language' headers
app.use(i18n.init);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'js');
app.engine('js', require('./services/express-react-views').createEngine());
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: false
}));
app.use(xmlparser({explicitArray: false, mergeAttrs: true}));
//serve API docs
app.use('/assets', express.static('assets'));
//use webpack middleware in local dev environment
if(process.env.NODE_ENV !== 'production'){
var webpackDevMiddleware = require("webpack-dev-middleware");
log.info('Dev: Using Webpack Dev Middleware');
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
stats:{
chunks: false,
timings: false,
assets: false,
modules: false,
children: false
}
}));
} else {
//serve static and client JS
log.info('Prod: using static public ');
app.use('/public', express.static(local.publicFilePath));
}
//load all route files using express-load
load('./routes').into(app);
//error handling
app.use(function(req, res, next) {
//bypass for dynamically created tile URLs
if(req.url.includes('/api/tiles/') || req.url.includes('/dialog/authorize/decision')){
next();
} else {
res.status(404);
if (req.accepts('html')) {
res.render('error', {
title: req.__('404: Page not found'),
props: {
title: req.__('404: Page not found'),
error: req.__('404: Page not found'),
url: req.url
},
/*eslint-disable*/
req: req
/*eslint-enable*/
});
return;
}
if (req.accepts('json')) {
res.send({
title: req.__('404: Page not found'),
error: req.__('404: Page not found'),
url: req.url
});
}
}
});
app.use(function(err, req, res, next) {
//bypass for dynamically created tile URLs
if(req.url.includes('/api/tiles/')){
next();
} else {
// curl https://localhost:4000/error/403 -vk
// curl https://localhost:4000/error/403 -vkH "Accept: application/json"
var statusCode = err.status || 500;
var statusText = '';
//var errorDetail = (process.env.NODE_ENV === 'production') ? '' : err.stack;
var errorDetail = err.stack;
switch (statusCode) {
case 400:
statusText = 'Bad Request';
break;
case 401:
statusText = 'Unauthorized';
break;
case 403:
statusText = 'Forbidden';
break;
case 500:
statusText = 'Internal Server Error';
break;
}
log.error(err.stack);
if (req.accepts('html')) {
res.status(statusCode).render('error', {
title: statusCode + ': ' + statusText,
props: {
title: statusCode + ': ' + statusText,
error: errorDetail,
url: req.url
}
});
return;
}
if (req.accepts('json')) {
res.status(statusCode).send({
title: statusCode + ': ' + statusText,
error: errorDetail,
url: req.url
});
}
}
});
var http = require('http');
var server = http.createServer(app);
server.setTimeout(10*60*1000); // 10 * 60 seconds * 1000 msecs
server.listen(local.internal_port, function () {
log.info('**** STARTING SERVER ****');
log.info('Server Running on port: ' + local.internal_port);
});