-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (58 loc) · 1.74 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
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var methodOverride = require('method-override');
var consign = require('consign');
var hbs = require('hbs');
var error = require('./middlewares/error');
var app = express();
require('dotenv').config();
var server = require('http').Server(app);
var port = process.env.PORT || 5777;
var io = require('socket.io')(server);
var cookie = cookieParser('ntalk');
var store = new session.MemoryStore();
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
app.use(methodOverride('_method'));
app.use(cookie);
app.use(session({
resave: true,
saveUninitialized: true,
key: process.env.SESSION_KEY,
secret: process.env.SESSION_SECRET,
store: store
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
hbs.registerPartials(__dirname + '/views/partials');
app.use(express.static(__dirname + '/public'));
io.use(function(socket, next) {
var data = socket.request;
cookie(data, {}, function(err) {
var sessionID = data.signedCookies[process.env.SESSION_KEY];
store.get(sessionID, function(err, session) {
if (err || !session) {
return next(new Error('Acesso negado'));
} else {
socket.handshake.session = session;
return next();
}
});
});
});
consign({verbose: false})
.include('models')
.then('controllers')
.then('routes')
.into(app);
consign({verbose: false})
.include('sockets')
.into(io);
app.use(error.notFound);
app.use(error.serverError);
server.listen(port, function() {
console.log('Running at http://localhost:' + port + '\n');
});
module.exports = app;