-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
108 lines (92 loc) · 2.79 KB
/
index.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
console.log('Importing...');
// MODULE IMPORTS
const cookieParser = require('cookie-parser');
const express = require('express');
const flash = require('express-flash-messages');
const session = require('express-session');
const http = require('http');
const enforce = require('express-sslify');
const { initializeAnalytics } = require('./analytics');
const path = require('path');
console.log('Setting up Express server...');
try {
require('dotenv').config();
} catch {
console.log(
'Not in development environment. Skipping loading from .env file.'
);
}
// START EXPRESS SERVER
const app = express();
const PORT = process.env.PORT || 3000;
const SESSION_SECRET = process.env.SESSION_SECRET || 'devsecret';
if (PORT != 3000) {
app.use(enforce.HTTPS({ trustProtoHeader: true }));
}
const mongo = require('./utils/functions/mongo.js');
app.use(
session({
secret: SESSION_SECRET,
resave: false,
saveUninitialized: true,
store: mongo.sessionStore,
})
);
require('./utils/functions/passport.js')(app, mongo);
app.use(flash()); // express-flash-messages config
app.use(cookieParser());
app.set('view engine', 'ejs');
// app.use(expressLayouts);
// app.set('layout', 'layouts/empty.ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res, next) => {
res.locals.successFlash = req.flash('successFlash');
res.locals.errorFlash = req.flash('errorFlash');
if (req.user) {
app.locals.darkMode = req.user.preferences.dark_mode || false;
} else {
app.locals.darkMode = false;
}
res.locals.logUser = req.user;
next();
});
// ROUTE IMPORTS
require('./routes/public.js')(app, mongo);
require('./routes/private.js')(app, mongo);
require('./routes/admin.js')(app, mongo);
require('./routes/usabo.js')(app, mongo);
require('./routes/class.js')(app, mongo);
require('./routes/contributors.js')(app, mongo);
require('./routes/references.js')(app, mongo);
require('./routes/settings.js')(app, mongo);
require('./routes/stats.js')(app, mongo);
require('./routes/train.js')(app, mongo);
require('./routes/usaboAdmin.js')(app, mongo);
require('./routes/usaboTrain.js')(app, mongo);
require('./routes/reviewers.js')(app, mongo);
// WILDCARD FOR ALL OTHER ROUTES
app.get('*', (req, res) => {
req.flash(
'errorFlash',
"Error 404: File Not Found. That page doesn't exist."
);
res.redirect('/');
});
// GLOBAL ERROR HANDLER
app.use((err, req, res, next) => {
if (res.headersSent) {
console.log('yes');
return next(err);
}
console.error(err.stack);
req.flash(
'errorFlash',
'Error 500: Internal Server Error. Something broke on our end, sorry about that.'
);
res.redirect('/');
});
// START http AND https SERVERS
http.createServer(app).listen(PORT, function () {
console.log('Express server listening on port ' + PORT);
});
initializeAnalytics();