-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
254 lines (233 loc) · 6.15 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* jshint undef:true, node: true, loopfunc: true, maxlen: 160, camelcase: false */
///<reference path='./node_modules/DefinitelyTyped/node/node.d.ts'/>
///<reference path='./node_modules/DefinitelyTyped/express/express.d.ts'/>
var express = require('express'),
apiRoutes = require('./routes/api'),
async = require('asyncawait/async'),
bodyParser = require('body-parser'),
compression = require('compression'),
config = require('config'),
cookieParser = require('cookie-parser'),
crypto = require('crypto'),
favicon = require('serve-favicon'),
morgan = require('morgan'),
path = require('path'),
routes = require('./routes/index'),
scmp = require('scmp'),
session = require('express-session'),
legEx = require('express-legacy-expires'),
assets = require('connect-assets'),
// ^ monkey-patched for autoprefixing. a bug has been filed on the connect-assets github page.
// See https://github.com/adunkman/connect-assets/issues/265
minify = require('html-minifier').minify,
fs = require('fs'),
helmet = require('helmet'),
httpStatus = require('http').STATUS_CODES,
sqliteStore = require('connect-sqlite3')({
session: session
});
require('sugar');
function fixEndings(str) {
// This might not really be necessary...
return config.unix ? str.replace(/\r\n/g, '\n') : str;
}
if (config.refreshWhit) {
// Speed up restarts locally
fs.writeFileSync(
config.whDir + '/WORD.MOD',
fixEndings(fs.readFileSync('words-config/WORD.MOD', {
encoding: 'utf8'
}))
);
fs.writeFileSync(
config.whDir + '/WORD.MDV',
fixEndings(fs.readFileSync('words-config/WORD.MDV', {
encoding: 'utf8'
}))
);
}
var app = express();
app.disable('x-powered-by');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(helmet.crossdomain());
app.use(helmet.xframe('DENY'));
app.use(helmet.nosniff());
app.use(helmet.ienoopen());
app.use(helmet.xssFilter());
if (config.https) {
app.use(helmet.hsts({
maxAge: 31536000000,
force: true
}));
}
app.use(helmet.csp({
defaultSrc: [
'\'self\'',
'www.google-analytics.com',
'https://stats.g.doubleclick.net',
'http://stats.g.doubleclick.net'
],
scriptSrc: [
'\'self\'',
'ajax.googleapis.com',
'cdnjs.cloudflare.com',
'www.google-analytics.com',
'cdn.jsdelivr.net',
'\'unsafe-inline\'',
'\'unsafe-eval\''
],
fontSrc: [
'fonts.gstatic.com',
'netdna.bootstrapcdn.com'
],
styleSrc: [
'\'self\'',
'fonts.googleapis.com',
'netdna.bootstrapcdn.com',
'\'unsafe-inline\''
]
}));
app.use(legEx());
app.use(compression());
app.use(cookieParser(config.sessionSecret));
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(morgan(config.prodLogFormat ? 'combined' : 'dev'));
app.use(bodyParser.json());
app.use(session({
store: new sqliteStore(),
secret: config.sessionSecret,
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 86400000 // To fix a bug in the old version of connect-sqlite3 I need to use (see the.todo)
}
}));
app.use(function(req, res, next) {
if (req.hostname != config.reqHost) {
res.redirect(config.mainURL);
} else {
next();
}
});
app.use(function(req, res, next) {
// Based on https://github.com/veeti/simple-csrf/blob/master/index.js
// Get/generate token
var token = req.cookies['XSRF-TOKEN'];
if (
req.method != 'GET' &&
req.method != 'HEAD' &&
req.method != 'OPTIONS' &&
!scmp(req.headers['x-xsrf-token'], token)
) {
var err = new Error('Invalid CSRF token.');
err.status = 403;
next(err);
} else {
if (!token) {
res.cookie('XSRF-TOKEN', crypto.randomBytes(32).toString('hex'));
}
next();
}
});
app.use(function(req, res, next) {
// Based on koa-html-minifier + modified from expressjs source
// For now, I've disabled this...not sure whether it would speed up anything
// b/c it takes time to minify things. Can't cache well b/c vars. in
// embedded JS are changing.
return next();
res.render = function(view, options, fn) {
options = options || {};
var self = this;
var req = this.req;
var app = req.app;
// support callback function as second arg
if ('function' == typeof options) {
fn = options;
options = {};
}
// merge res.locals
options._locals = self.locals;
// default callback to respond
fn = fn || function(err, str) {
if (err) {
return req.next(err);
}
str = minify(str, { // No caching?
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeOptionalTags: true,
processScripts: ['text/ng-template']
});
self.send(str);
};
// render
app.render(view, options, fn);
};
next();
});
app.use(assets({
paths: [
'public',
'node_modules/inobounce',
'node_modules/angularLocalStorage/src',
'node_modules/angulartics/src',
'node_modules/bootstrap_dropdowns_enhancement/dist/css',
'node_modules/angular-bootstrap-switch/dist',
'node_modules/bootstrap-switch/dist/js',
'node_modules/bootstrap-switch/dist/css/bootstrap3',
'node_modules/bootstrap/js',
'node_modules/jquery-scrollparent'
],
build: config.minify,
buildDir: 'build_cache',
compress: config.minify,
servePath: config.minify ? 'loquax/assets' : 'assets',
precompile: ['*.js', '*.less', '*.css', '*.png'],
compile: true,
gzip: false,
prefixCss: true
}));
app.use('/static', express.static(__dirname + '/static'));
// For all non-static stuff, don't cache at all.
app.use(helmet.nocache());
app.use(function(req, res, next) {
async(next)();
});
app.use(config.prefix + '/', routes);
app.use(config.prefix + '/', apiRoutes);
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
app.use(function(err, req, res, next) {
if (config.stackTrace) {
console.log('Error:', err.message, err.stack);
}
if (typeof err == 'string') {
err = {
stack: 'String message...',
message: err
};
}
err.status = err.status || 500;
res.status(err.status);
res.render('error', config.stackTrace ? {
message: err.message,
error: err
} : {
message: httpStatus[err.status], // Be more generic
error: {
status: err.status,
stack: ''
}
});
});
app.listen(3000);
module.exports = app;