-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (66 loc) · 2.29 KB
/
server.js
File metadata and controls
87 lines (66 loc) · 2.29 KB
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
const express = require('express');
const { connectMongo : connectToMongoDB } = require('./config/db');
const dotenv = require('dotenv');
const { Server } = require('socket.io');
const http = require('http');
const cors = require('cors');
dotenv.config();
const path = require('path');
const rateLimiter = require('./middleware/rate-limiter');
const { redisClient } = require('./config/redis');
const app = express();
const httpServer = http.createServer(app);
//Connect to MongoDB
connectToMongoDB();
//Intiliazing middleware
app.use(express.json({ extended: false }));
app.use(cors());
app.use(rateLimiter);
const EXPRESS_SERVER_PORT = process.env.EXPRESS_SERVER_PORT;
app.get('/', (req, res) => {
res.send({ message: 'API Working fine' });
})
//Initializing socket.io
const io = new Server(httpServer,
{
cors: {
origin: '*',
methods: ['GET', 'POST'],
credentials: true
}}
)
io.on('connection', socket => {
console.log('CONNECTED!');
socket.on('get-document', documentInfo => {
socket.join(documentInfo.documentId);
socket.emit('load-document', documentInfo.data);
})
socket.on('send-changes', documentInfo => {
socket.broadcast.to(documentInfo.documentId).emit('receive-changes', documentInfo.delta);
})
socket.on('save-document', documentInfo => {
socket.broadcast.to(documentInfo.documentId).emit('save-document', documentInfo.data);
})
// socket.on('user-joined', ({ username, documentId }) => {
// redisClient.sAdd(documentId, username);
// })
})
//Defining routes
app.use('/api/auth', require('./routes/api/auth'));
app.use('/api/users', require('./routes/api/users'));
app.use('/api/documents', require('./routes/api/document'));
app.use('/api/profile', require('./routes/api/profile'));
//Serve static assets in production
// if (process.env.NODE_ENV === 'production') {
// //Set static folder
// app.use(express.static('client/build'));
// app.get('*', (req, res) => {
// res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
// })
// }
httpServer.listen(EXPRESS_SERVER_PORT, () => {
console.log('Server listening on port', EXPRESS_SERVER_PORT);
app.emit('server-started');
});
app.on('close', () => httpServer.close());
module.exports = app;