-
Notifications
You must be signed in to change notification settings - Fork 42
/
server.js
85 lines (77 loc) · 2.02 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
74
75
76
77
78
79
80
81
82
83
84
85
const express = require("express");
const app = express();
const MongoClient = require("mongodb").MongoClient;
const PORT = 2121;
require("dotenv").config();
let db,
dbConnectionStr = process.env.DB_STRING,
dbName = "rap";
MongoClient.connect(dbConnectionStr, { useUnifiedTopology: true }).then(
(client) => {
console.log(`Connected to ${dbName} Database`);
db = client.db(dbName);
}
);
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get("/", (request, response) => {
db.collection("rappers")
.find()
.sort({ likes: -1 })
.toArray()
.then((data) => {
response.render("index.ejs", { info: data });
})
.catch((error) => console.error(error));
});
app.post("/addRapper", (request, response) => {
db.collection("rappers")
.insertOne({
stageName: request.body.stageName,
birthName: request.body.birthName,
likes: 0,
})
.then((result) => {
console.log("Rapper Added");
response.redirect("/");
})
.catch((error) => console.error(error));
});
app.put("/addOneLike", (request, response) => {
db.collection("rappers")
.updateOne(
{
stageName: request.body.stageNameS,
birthName: request.body.birthNameS,
likes: request.body.likesS,
},
{
$set: {
likes: request.body.likesS + 1,
},
},
{
sort: { _id: -1 },
upsert: true,
}
)
.then((result) => {
console.log("Added One Like");
response.json("Like Added");
})
.catch((error) => console.error(error));
});
app.delete("/deleteRapper", (request, response) => {
db.collection("rappers")
.deleteOne({ stageName: request.body.stageNameS })
.then((result) => {
console.log("Rapper Deleted");
response.json("Rapper Deleted");
})
.catch((error) => console.error(error));
});
app.listen(process.env.PORT || PORT, () => {
console.log(`Server running on port ${PORT}`);
});