-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
90 lines (76 loc) · 2.72 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
84
85
86
87
88
89
90
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const passport = require('passport');
const flash = require('express-flash');
const hbs = require('hbs');
const expressHbs = require('express-handlebars');
const passportSocketIo = require("passport.socketio");
const config = require('./config/secret');
const sessionStore = new MongoStore({url: config.database, autoReconnect: true});
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const sessionMiddleware = session({
resave: true,
saveUninitialized: true,
secret: config.secret,
store: new MongoStore({url: config.database, autoReconnect: true})
})
mongoose.connect(config.database, {
useMongoClient: true
}, function (err) {
if (err)
console.log(err);
console.log("Connected to the database");
});
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(sessionMiddleware);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(function (req, res, next) {
res.locals.user = req.user;
next();
});
io.use(passportSocketIo.authorize({
cookieParser: cookieParser, // the same middleware you registrer in express
key: 'connect.sid', // the name of the cookie where express/connect stores its session_id
secret: config.secret, // the session_secret to parse the cookie
store: sessionStore, // we NEED to use a sessionstore. no memorystore please
success: onAuthorizeSuccess, // *optional* callback on success - read more below
fail: onAuthorizeFail, // *optional* callback on fail/error - read more below
}));
io.use((socket, next) => {
sessionMiddleware(socket.request, socket.request.res, next);
})
function onAuthorizeSuccess(data, accept) {
console.log('successful connection to socket.io');
accept();
}
function onAuthorizeFail(data, message, error, accept) {
console.log('failed connection to socket.io:', message);
if (error)
accept(new Error(message));
}
require('./realtime/io')(io);
const mainRoutes = require('./routes/main');
const orderRoutes = require('./routes/order');
const userRoutes = require('./routes/user');
app.use(mainRoutes);
app.use(orderRoutes);
app.use(userRoutes);
var server = http.listen(port, (err) => {
if (err)
console.log(err);
console.log(`Running on port ${config.port}`);
});