-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
99 lines (79 loc) · 2.25 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
91
92
93
94
95
96
97
require('dotenv').config()
const express=require('express')
const app=express()
const ejs=require('ejs')
const path=require('path')
const expressLayout=require('express-ejs-layouts')
const Emitter = require('events')
const DB_CONNECTION_URL=process.env.MONGO_CONNECTION_URL
const COOKIE_SECRET=process.env.COOKIE_SECRET
const PORT=process.env.PORT;
const mongoose=require('mongoose')
const session=require('express-session')
const flash=require('express-flash')
const MongoDbStore = require('connect-mongo')(session)
const passport = require('passport')
// Database Connection
mongoose.connect(DB_CONNECTION_URL,{ useNewUrlParser:true,useCreateIndex:true,useUnifiedTopology:true,useFindAndModify:true
});
const connection = mongoose.connection;
connection.once('open',()=>{
console.log('Database connected...');
}).catch(err => {
console.log('Connection failed...');
});
// session store
let mongoStore=new MongoDbStore({
mongooseConnection:connection,
collection:'sessions'
})
//Event Emitter
const eventEmitter = new Emitter();
app.set('eventEmitter',eventEmitter);
// session config
app.use(session({
secret:COOKIE_SECRET,
resave:false,
store:mongoStore,
saveUninitialized:false,
cookie:{maxAge:1000*60*60*24} // 24 hours
}))
//passport config
const passportInit = require('./app/config/passport')
passportInit(passport)
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
// Assets folder
app.use(express.static('public'))
app.use(express.urlencoded({extended:false}))
app.use(express.json())
app.use((req,res,next) => {
res.locals.session=req.session
res.locals.user=req.user
next()
})
// set Template Engine
app.use(expressLayout)
app.set('views',path.join(__dirname,'/resources/views'))
app.set('view engine','ejs')
// routes
require('./routes/web')(app)
app.use((req,res)=>{
res.status(404).render('errors/404')
})
const server = app.listen(PORT,()=>{
console.log(`Listening on port ${PORT}`);
})
const io = require('socket.io')(server)
io.on('connection',(socket)=>{
socket.on('join',(orderId)=>{
socket.join(orderId)
})
})
eventEmitter.on('orderUpdated',(data)=>{
io.to(`order_${data.id}`).emit('orderUpdated',data)
})
eventEmitter.on('orderPlaced',(data)=>{
io.to('adminRoom').emit('orderPlaced',data)
})