-
Notifications
You must be signed in to change notification settings - Fork 61
/
app.js
95 lines (86 loc) · 2.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
'use strict';
const banHandler = require('./lib/middleware/ban-handler');
const errorHandler = require('./lib/middleware/error-handler');
const express = require('express');
const favicon = require('serve-favicon');
const http = require('http');
const port = require('./config').port;
const session = require('express-session');
const RedisStore = require('connect-redis').default;
const secret = process.env.SITE_SECRET || 'shhhh, very secret';
const cookieParser = require('cookie-parser')(secret);
const site = require('./routes/site');
const urlencoded = require('body-parser').urlencoded;
const user = require('./routes/user');
const usersdb = require('./lib/redis-clients').users;
/**
* Setting up Express.
*/
const app = express();
const production = process.env.NODE_ENV === 'production';
const pub = __dirname + '/public'; // Path to public directory
const sessionstore = new RedisStore({ client: usersdb });
const server = http.createServer(app); // HTTP server object
// Configuration
app.set('view engine', 'pug');
app.use('/static', express.static(pub, { maxAge: 2419200000 })); // 4 weeks = 2419200000 ms
app.use(favicon(pub + '/img/favicon.ico', { maxAge: 2419200000 }));
app.use(banHandler);
app.use(urlencoded({ extended: false }));
app.use(cookieParser);
app.use(
session({
cookie: {
secure: production,
maxAge: 14400000 // 4 h = 14400000 ms
},
proxy: production,
resave: false,
rolling: true,
saveUninitialized: true,
secret: secret,
store: sessionstore
})
);
// Routes
app.get('/', site.home);
app.get('/artworks', site.artworks);
app.get('/changepasswd', site.validationErrors, site.changePasswd);
app.post(
'/changepasswd',
user.validateChangePasswd,
user.checkOldPasswd,
user.changePasswd
);
app.get('/leaderboards', user.leaderboards);
app.get('/login', site.validationErrors, site.login);
app.post('/login', user.validateLogin, user.checkUser, user.authenticate);
app.get('/logout', user.logout);
app.get('/recoverpasswd', site.validationErrors, site.recoverPasswd);
app.post('/recoverpasswd', user.validateRecoverPasswd, user.sendEmail);
app.get('/resetpasswd', site.validationErrors, site.resetPasswd);
app.post('/resetpasswd', user.resetPasswd);
app.get('/sliceleaderboard', user.sliceLeaderboard);
app.get('/signup', site.validationErrors, site.signup);
app.post(
'/signup',
user.validateSignUp,
user.userExists,
user.emailExists,
user.createAccount
);
app.get('/:room', site.room);
app.get('/user/:username', user.profile);
app.use(errorHandler);
/**
* Setting up the rooms.
*/
require('./lib/rooms')({
parser: cookieParser,
server: server,
sessionstore: sessionstore
});
// Begin accepting connections
server.listen(port, function() {
console.info('binb server listening on port ' + port);
});