-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (40 loc) · 1.2 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
// Required Modules
const express = require('express');
const app = express();
const http = require('http');
const cors = require('cors');
const path = require('path');
const dotenv = require('dotenv');
const server = http.createServer(app); // Creating Server
// Required Socket.io
const { Server } = require('socket.io');
dotenv.config({path: "./config.env"})
const PORT = process.env.PORT || 8080 // Port Number
app.use(cors());
const io = new Server(server, {
cors: {
origin: "https://zsn-chat.herokuapp.com/", // Cors Allows This Site
methods: ["GET", "POST"] // This methods will work
}
})
let allMessages = []
io.on('connection', (socket) => {
socket.on('chat', (value) => { // Getting Values From Client
allMessages.push(value)
io.emit('display', allMessages) // Values Sent To Client
})
})
setInterval(() => {
allMessages = []
}, 200000)
// App Deployment Code
if(process.env.NODE_ENV === 'production'){
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
// Server Listening
server.listen(PORT, () => {
console.log("Server Running...")
})