-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
69 lines (57 loc) · 1.64 KB
/
Copy pathapp.js
File metadata and controls
69 lines (57 loc) · 1.64 KB
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
const KEY = 'ntalk.sid', SECRET = 'ntalk';
const express = require('express');
const path = require('path');
const consign = require('consign');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const expressSession = require('express-session');
const methodOverride = require('method-override');
const error = require('./middlewares/error');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const cookie = cookieParser(SECRET);
const store = new expressSession.MemoryStore();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(cookie);
app.use(expressSession({
secret: SECRET,
name: KEY,
resave: true,
saveUninitialized: true,
store: store
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride('_method'));
app.use(express.static(path.join(__dirname, 'public')));
io.use(function(socket, next) {
const data = socket.request;
cookie(data, {}, function(err) {
const sessionID = data.signedCookies[KEY];
store.get(sessionID, function(err, session) {
if(err || !session) {
return next(new Error('Acesso Negado'));
} else {
socket.handshake.session = session;
return next();
}
})
})
});
consign({})
.include('models')
.then('controllers')
.then('routes')
.into(app)
;
app.use(error.notFound);
app.use(error.serverError);
consign({})
.include('sockets')
.into(io)
;
server.listen(3000, () => {
console.log('Ntalk no ar.');
});