-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (43 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
const express = require("express");
const mongoose= require("mongoose");
const bodyParser = require('body-parser');
const app = express();
require('dotenv').config();
const server = require("http").Server(app);
const ab = require("socket.io");
const { v4: uuidv4 } = require("uuid");
app.set("view engine", "ejs");
const io = require("socket.io")(server, {
cors: {
origin: '*'
}
});
const { ExpressPeerServer } = require("peer");
const peerServer = ExpressPeerServer(server, {
debug: true,
});
if (typeof localStorage === "undefined" || localStorage === null) {
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
}
mongoose.connect( process.env.DB_URL,
{ useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
mongoose.set("useCreateIndex", true);
mongoose.set('useFindAndModify', false);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use("/peerjs", peerServer);
app.use(express.static("public"));
app.use('/', require('./routes/index'));
io.on("connection", (socket) => {
socket.on("join-room", (roomId, userId, userName) => {
socket.join(roomId);
socket.to(roomId).broadcast.emit("user-connected", userId);
socket.on("message", (message) => {
io.to(roomId).emit("createMessage", message, userName);
});
});
});
server.listen(process.env.PORT || 3030);