-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (70 loc) · 2.24 KB
/
server.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
const express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
hbs = require('hbs'),
expressHbs = require('express-handlebars'),
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
flash = require('express-flash'),
passport = require('passport'),
passportSocketIo = require('passport.socketio'),
cookieParser = require('cookie-parser'),
config = require('./config/secret'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http);
mongoose.Promise = global.Promise;
var promise = mongoose.connect(config.database,{useMongoClient: true}, function(err){
if(err){
console.log(err)
}else{
console.log('connected to new database');
}
})
const sessionStore = new MongoStore({ url: config.database, autoReconnect: true});
app.engine('.hbs', expressHbs({ defaultLayout: 'layout', extname: '.hbs' }));
app.set('view engine', 'hbs');
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
resave: true,
saveUninitialized: true,
secret: config.secret,
store: sessionStore
}));
app.use(flash());
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next){
res.locals.user = req.user;
next();
});
io.use(passportSocketIo.authorize({
cookieParser: cookieParser,
key: 'connect.sid',
secret: config.secret,
store: sessionStore,
success: onAuthorizeSuccess,
fail: onAuthorizeFail
}));
function onAuthorizeSuccess(data, accept){
console.log('successful connection');
accept();
}
function onAuthorizeFail(data, message, error, accept){
console.log("failed connection");
if(error) accept(new Error(message));
}
require('./realtime/io')(io);
const mainRoutes = require('./routes/main');
const userRoutes = require('./routes/user');
app.use(mainRoutes);
app.use(userRoutes);
http.listen(3000, (err) =>{
if(err) console.log(err);
console.log('Running on port ${3000}')
});