-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (48 loc) · 1.44 KB
/
server.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
const express = require("express");
const cors = require("cors");
const app = express();
const server = require('http').Server(app) // server for socket io
const io = require('socket.io')(server,{
cors: {
origin: "*",
methods: ["GET", "POST"]
}
})
const { v4: uuidV4 } = require('uuid')
const PORT = process.env.PORT || 5100;
/**
* Middle Ware
*/
app.use(cors()); // Handles cross orign request errors.
app.use(express.json()); // Converts the request body into json object from string
app.use(express.urlencoded({ extended: true })); // Understand fetch requests
app.use(express.static("public/build")); // for pushing onto heroku
app.use(express.static("public")); // for pushing onto herokux
/**
* Routes
*/
app.use("/home", (req, res) => {
res.send({ msg: "Hello", roomId: uuidV4() });
});
app.get('/', (req, res) => {
res.redirect(`/create${uuidV4()}`)
})
app.get('/create:create', (req, res) => {
res.render('/create', { roomId:req.params.room } )
})
io.on('connection', socket => {
socket.on('join-room', (room) => {
socket.join(room.url)
socket.broadcast.to(room.url).emit("user-connected", room);
console.log(room.url);
socket.on('disconnect', ()=>{
socket.broadcast.to(room.url).emit("user-disconnected", room);
})
})
})
/**
* Listening
*/
server.listen(PORT, () =>{
console.log(`Sever running on ${PORT}`)
});