-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (60 loc) · 1.74 KB
/
Copy pathserver.js
File metadata and controls
80 lines (60 loc) · 1.74 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
const express = require('express')
const userRoutes = require('./routes/userRoutes')
const projectRoutes = require('./routes/projectRoutes')
const taskRoutes = require("./routes/taskRoutes")
const cors = require("cors")
require('dotenv').config();
require('./config/database');
const app = express()
app.use(express.json())
//CORS CONFIGURATION
const whiteList = [process.env.FRONTEND_URL];
const corsOption = {
origin: function(origin, callback){
if(whiteList.includes(origin)){
callback(null, true)
}else{
callback(new Error("CORS error"))
}
}
}
app.use(cors(corsOption))
//Routing
app.use('/users', userRoutes)
app.use('/projects', projectRoutes)
app.use('/task', taskRoutes)
const PORT = process.env.PORT || 4000
const server = app.listen(PORT, ()=>{
console.log(`Server listen on port ${PORT}`)
})
//socket.io
const { Server } = require('socket.io')
const io = new Server(server,{
pingTimeout: 60000,
cors: {
origin: process.env.FRONTEND_URL,
},
})
io.on("connection", (socket) =>{
// console.log("connected to socket.io")
//Socket.io events
socket.on('open project', (project)=>{
socket.join(project)
});
socket.on('new task', (task) =>{
const project = task.project
socket.to(project).emit('added task', task)
});
socket.on("delete task", task =>{
const project = task.project
socket.to(project).emit("deleted task", task)
});
socket.on("update task", task => {
const project = task.project._id
socket.to(project).emit("updated task", task)
});
socket.on("change status", task =>{
const project = task.project._id
socket.to(project).emit("new state", task)
})
})