-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (63 loc) · 2.01 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const passport = require('passport');
const dotenv = require('dotenv');
const users = require('./routes/users');
const messages = require('./routes/messages');
const groups = require('./routes/groupchat');
var clients = require('./utils/clientData');
var path = require('path');
dotenv.config();
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.options('*', cors());
const port = process.env.PORT || 5000;
const server = app.listen(port, () => {
console.log('Server is up on port' + port);
});
const io = require('socket.io')(server);
io.on("connection", (socket) => {
socket.on('clientInfo', (data) => {
clients[data.id ?? JSON.parse(data).id] = socket.id;
io.emit("userlist", Object.keys(clients));
});
socket.on('disconnect', () => {
for (let i in clients) {
if (clients[i] === socket.id) delete clients[i];
}
io.emit("userlist", Object.keys(clients));
for (let i in socket.rooms) socket.leave(i);
});
socket.on('joingroup', (item) => {
socket.join(item);
});
socket.on('endgroup', (item) => {
socket.leave(item);
});
});
mongoose.connect(process.env.mongoDB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
})
.then(() => console.log('MongoDB successfully connected'))
.catch((e) => console.log(e));
app.use(passport.initialize());
require('./config/passport')(passport);
app.use(function (req, res, next) {
req.io = io;
next();
});
app.use('/api/users', users);
app.use('/api/messages', messages);
app.use('/api/group', groups);
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
}