-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
124 lines (112 loc) · 3.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const compression = require('compression');
const express = require('express');
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
const path = require('path'); // what is this
const bodyParser = require('body-parser');
const http = require('http');
const cors = require('cors');
const flash = require('express-flash');
const session = require('express-session');
const morgan = require('morgan');
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const indexRoute = require('./routes/index/index');
const authRoute = require('./routes/auth');
const friendRoute = require('./routes/friend');
const profileRoute = require('./routes/account/profile');
const editProfileRoute = require('./routes/account/editProfile');
const chatRoute = require('./routes/chat');
const notificationRoute = require('./routes/notification')
const aboutRoute = require('./routes/about');
const feedbackRoute = require('./routes/feedback');
const errorsRoute = require('./routes/errors');
const app = express();
const server = http.createServer(app)
const io = require('socket.io')(server);
const Room = require('./models/Room');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(compression());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(express.static('public'));
// app.use(cookieParser());
app.set('trust proxy', 1);
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: false, maxAge: 3600000 } // todo set true if hosting
})
);
app.use(flash());
app.use((req, res, next) => {
req.io = io;
next();
})
app.use('/', indexRoute);
app.use('/auth', authRoute);
app.use('/friend', friendRoute);
app.use('/account', profileRoute);
app.use('/account/edit', editProfileRoute);
app.use('/chat', chatRoute);
app.use('/notification', notificationRoute);
app.use('/about', aboutRoute);
app.use('/feedback', feedbackRoute)
app.use('*', errorsRoute);
io.on('connection', socket => {
socket.on('new-user', async (room, name) => {
socket.join(room)
await Room.updateOne({ $and: [{ name: room }, { users: { $elemMatch: {username: name} }}] },
{
$set: { "users.$.socketId" : socket.id }
})
socket.to(room).broadcast.emit('user-connected', name)
})
socket.on('send-chat-message', async (room, message, name) => {
await Room.updateOne({ name: room }, {
$push: {chats: { message: message, username: name, date: Date.now()}}
})
socket.to(room).broadcast.emit('chat-message', message, name)
})
socket.on('join-call', (room, name) => {
console.log('join call');
socket.join(room)
socket.to(room).broadcast.emit('user-joined-call', name)
})
socket.on('disconnect', async () => {
const userData = await Room.findOne({ users: { $elemMatch: { socketId: socket.id}}}, {
users: {
$elemMatch: {
socketId: socket.id
}
}, name: 1
})
if (userData){
socket.to(userData.name).broadcast.emit('user-disconnected', userData.users[0].username)
await Room.updateOne({ name: userData.name }, {
$pull: {
users: { username: userData.users[0].username }
}
})
}
})
})
// Connect to DB
mongoose.connect(
process.env.DB_CONNECTION, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
},
() => console.log('connected to DB')
);
server.listen(process.env.PORT, () => console.log(`Server has started on port ${process.env.PORT}`));
module.exports = app;