-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (47 loc) · 1.39 KB
/
index.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
const express = require('express')
const setUpDB = require('./config/database')
const router = require('./config/routes')
const cors = require('cors')
const path = require('path')
const socket = require('socket.io')
const port = process.env.PORT || 3020
const app = express()
setUpDB()
// const corsOptions = {
// exposedHeaders: 'x-auth',
// };
// app.use(cors(corsOptions))
const whitelist = ['http://localhost:3000'];
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}, credentials: true
}
app.use(cors(corsOptions))
app.use(express.json())
app.use('/', router)
app.use(express.static(path.join(__dirname,"client/build")))
app.get("*",(req,res) => {
res.sendFile(path.join(__dirname + "/client/build/index.html"))
})
const server = app.listen(port, () => {
console.log('listening on the port', port)
})
module.exports = server
const io = socket(server)
io.on('connection', function(socket){
console.log('a user connected');
socket.on('create_request', function(data){
console.log('in backend', data)
io.emit('create_request', data)
})
socket.on('modify_request', function(data){
console.log('in backend',data)
io.emit('modify_request', data)
})
});
module.exports = io