-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (54 loc) · 1.56 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
65
66
67
68
69
70
71
72
73
const express = require("express");
const document = require("./models/document");
require("dotenv").config();
const app = express();
// Connect DB
async function makeDBConnection() {
const connectDB = require("./config/db");
await connectDB();
}
makeDBConnection();
const server = require("http").createServer(app);
const io = require("socket.io")(server);
const path = require("path");
app.use(express.static(path.join(__dirname, "static")));
// Routes
app.get("/", (req, res) => {
res.sendFile(__dirname + "/static/home.html");
});
app.get("/error", (req, res) => {
res.sendFile(__dirname + "/static/error.html");
});
app.get("/:id", (req, res) => {
res.sendFile(__dirname + "/static/document.html");
});
io.on("connection", (socket) => {
socket.on("get-document", async (id) => {
console.log("Get document");
const doc = await findOrCreateDocument(id);
socket.join(id);
socket.emit("load-document", doc.data);
socket.on("send-changes", (delta) => {
socket.broadcast.to(id).emit("receive-changes", delta);
});
socket.on("save-document", async (data) => {
await document.findByIdAndUpdate(id, { data });
});
});
console.log("Connected");
});
const PORT = process.env.PORT || 8080;
server.listen(PORT);
// DB Controller
const defaultValue = "";
async function findOrCreateDocument(id) {
if (id == null) {
console.log("Id is null");
return;
}
console.log("Searching for the doc");
const doc = await document.findById(id);
console.log("Doc searched");
if (doc) return doc;
return await document.create({ _id: id, data: defaultValue });
}