forked from cmda-minor-web/real-time-web-2223
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (65 loc) · 1.97 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const path = require("path");
require("dotenv").config();
const io = require("socket.io")(http);
const port = process.env.PORT || 4242;
const { MongoClient } = require("mongodb");
const url = `mongodb+srv://${process.env.USERNAME}:${process.env.PASSWORD}@database.p67zuca.mongodb.net/?retryWrites=true&w=majority`;
const dbName = "db";
let mongoClient;
async function addQuoteToDatabase(quote) {
try {
const db = mongoClient.db(dbName);
const collection = db.collection("quotes");
await collection.insertOne({ quote: quote });
console.log("Quote toegevoegd aan de database.");
} catch (error) {
console.error(
"Fout bij het toevoegen van de quote aan de database:",
error
);
}
}
async function getQuotesFromDatabase() {
try {
const db = mongoClient.db(dbName);
const collection = db.collection("quotes");
const quotes = await collection.find().toArray();
return quotes;
} catch (error) {
console.error("Fout bij het ophalen van de quotes uit de database:", error);
throw error;
}
}
app.use(express.static(path.resolve("public")));
io.on("connection", (socket) => {
console.log("a user connected");
socket.on("message", async (message) => {
await addQuoteToDatabase(message);
io.emit("message", message);
});
socket.on("disconnect", () => {
console.log("user disconnected");
});
});
app.get("/quotes", async (req, res) => {
try {
const quotes = await getQuotesFromDatabase();
res.json(quotes);
} catch (error) {
res.status(500).json({ error: "Er is een fout opgetreden" });
}
});
MongoClient.connect(url)
.then((client) => {
mongoClient = client;
console.log("Verbonden met de MongoDB-server.");
})
.catch((error) => {
console.error("Fout bij het verbinden met de MongoDB-server:", error);
});
http.listen(port, () => {
console.log("Luisteren op poort", port);
});