-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
92 lines (78 loc) · 2.48 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
const express = require('express');
const path = require('path');
const http = require('http');
const socketIO = require('socket.io');
const consign = require('consign');
const bodyParser = require('body-parser');
const csurf = require('csurf');
const cookie = require('cookie');
const session = require('express-session');
const compression = require('compression');
const methodOverride = require('method-override');
const config = require('./config');
const error = require('./middlewares/error');
const redis = require('redis')
const redisAdapter = require('socket.io-redis');
let RedisStore = require('connect-redis')(session);
let redisClient = redis.createClient(config.redis);
const app = express();
app.disable('x-powered-by');
const server = http.Server(app);
const io = socketIO(server);
//const store = new session.MemoryStore();
const store = new RedisStore({client: redisClient, prefix: config.sessionKey});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('trust proxy', 1) // trust first proxy
app.use(compression());
app.use(session({
store: store,
name: config.sessionKey,
secret: config.sessionSecret,
resave: false,
saveUninitialized: false,
cookie: { secure: false }
}))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(methodOverride("_method"));
app.use(express.static(path.join(__dirname, 'public'), {maxAge: 3600000}));
app.use(
process.env.NODE_ENV.trim() === 'test' ? csurf({ ignoreMethods: ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'DELETE'] }): csurf()
);
app.use((req, res, next) => {
res.locals._csrf = req.csrfToken();
next();
});
io.adapter(redisAdapter());
//handle session in socket
io.use((socket, next) => {
const cookieData = socket.request.headers.cookie;
const cookieObj = cookie.parse(cookieData);
const sessionHash = cookieObj[config.sessionKey] || '';
const sessionID = sessionHash.split('.')[0].slice(2);
//With memory management was store.all
store.get((err, sessions) => {
const currentSession = sessions[sessionID];
if (err || !currentSession) {
return next(new Error('access denied!'));
}
socket.handshake.session = currentSession;
return next();
});
});
consign({})
.include('models')
.then('controllers')
.then('routes')
.then('events')
.into(app, io);
//middlewares de error
app.use(error.notFound);
app.use(error.serverError);
server.listen(3000, () => {
console.log('Habla running!');
});
module.exports = app;