-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (68 loc) · 1.79 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
/* eslint-disable no-console */
const express = require("express");
const bodyParser = require("body-parser");
const { MongoClient } = require("mongodb");
const { username, password, port } = require("./config");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
let db;
const url = `mongodb://${username}:${password}@ds263448.mlab.com:63448/quotes`;
MongoClient.connect(url, (err, database) => {
if (err) {
return console.log(err);
}
db = database.db("quotes");
return app.listen(port || 3000, () => {
console.log(`listening on ${port}`);
});
});
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.json());
app.get("/", (req, res) => {
db.collection("quotes")
.find()
.toArray((err, result) => {
if (err) return console.log(err);
// renders index.ejs
return res.render("index.ejs", { quotes: result });
});
});
app.post("/quotes", (req, res) => {
db.collection("quotes").save(req.body, err => {
if (err) return console.log(err);
console.log("saved to database");
return res.redirect("/");
});
});
app.put("/quotes", (req, res) => {
db.collection("quotes").findOneAndUpdate(
{ name: "Yoda" },
{
$set: {
name: req.body.name,
quote: req.body.quote
}
},
{
sort: { _id: -1 },
upsert: true
},
(err, result) => {
if (err) return res.send(err);
res.send(result);
}
);
});
app.delete("/quotes", (req, res) => {
// Handle delete event here
db.collection("quotes").findOneAndDelete(
{ name: req.body.name },
(err, result) => {
if (err) return res.send(500, err);
res.send({ message: "A darth vadar quote got deleted" });
}
);
});