forked from hajoxx/moneypot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
140 lines (115 loc) · 3.69 KB
/
server.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
var assert = require('assert');
var compression = require('compression');
var express = require('express');
var fs = require('fs');
var http = require('http');
var path = require('path');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = express();
var routes = require('./server/routes');
var database = require('./server/database');
var lib = require('./server/lib');
var dotCaching = true;
var _ = require('lodash');
// Simplify and de-verbosify timeago output.
var timeago = require('timeago');
var timeago_strings = _.extend(timeago.settings.strings, {
seconds: '< 1 min',
minute: '1 min',
minutes: '%d mins',
hour: '1 hour',
hours: '%d hours',
day: '1 day',
days: '%d days',
month: '1 month',
months: '%d months',
year: '1 year',
years: '%d years'
});
timeago.settings.strings = timeago_strings;
app.use(bodyParser());
app.use(cookieParser());
app.set("views", path.join(__dirname, '/views'));
if (process.env.NODE_ENV !== 'production') {
app.locals.pretty = true;
dotCaching = false;
}
app.engine("html", require("dot-emc").init(
{
app: app,
fileExtension:"html",
options: {
templateSettings: {
cache: dotCaching
}
}
}
).__express);
app.set("view engine", "html");
app.disable('x-powered-by');
app.enable('trust proxy');
app.use(compression());
var twoWeeksInSeconds = 1209600;
app.use(express.static(path.join(__dirname, '/client'), { maxAge: twoWeeksInSeconds * 1000 }));
app.use(function(req, res, next) {
var sessionId = req.cookies.id;
if (!sessionId) {
res.header('Vary', 'Accept, Accept-Encoding, Cookie');
res.header('Cache-Control', 'public, max-age=60'); // Cache the logged-out version
return next();
}
res.header('Cache-Control', 'no-cache');
if (!lib.isUUIDv4(sessionId)) {
res.clearCookie('id');
return next();
}
database.getUserBySessionId(sessionId, function(err, user) {
//The error is handled manually to avoid sending it into routes
if (err) {
if (err === 'NOT_VALID_SESSION') {
res.clearCookie('id');
return res.redirect('/');
} else {
console.error('[INTERNAL_ERROR] Unable to get user by session id ' + sessionId + ':', err);
return res.redirect('/error');
}
}
user.advice = req.query.m;
user.error = req.query.err;
user.eligible = lib.isEligibleForGiveAway(user.last_giveaway);
user.admin = user.userclass === 'admin';
user.moderator = user.userclass === 'admin' ||
user.userclass === 'moderator';
req.user = user;
next();
});
});
/**
* How to handle the errors:
* If the error is a string: Send it to the client.
* If the error is an actual: error print it to the server log.
*
* We do not use next() to avoid sending error logs to the client
* so this should be the last middleware in express .
*/
function errorHandler(err, req, res, next) {
if (err) {
if(typeof err === 'string') {
return res.render('error', { error: err });
} else {
if (err.stack) {
console.error('[INTERNAL_ERROR] ', err.stack);
} else console.error('[INTERNAL_ERROR', err);
res.render('error');
}
} else {
console.warning("A 'next()' call was made without arguments, if this an error or a msg to the client?");
}
}
routes(app);
app.use(errorHandler);
var port = process.env.PORT || 3841;
var server = http.createServer(app).listen(port, function() {
console.log('Listening on port ', port);
});