-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
45 lines (36 loc) · 1.14 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
const express = require("express");
const app = express();
const parser = require("body-parser");
const port = 3001;
app.use(parser.json());
app.use(parser.urlencoded({ extended: true }));
const MongoClient = require("mongodb").MongoClient;
MongoClient.connect(
"mongodb://localhost:27017",
(err, client) => {
if (err) next(err);
const db = client.db("favouritesDB");
console.log("Connected to db...");
app.get("/api/favourites", (req, res, next) => {
const favouritesCollection = db.collection("favourites");
favouritesCollection.find().toArray((err, favourites) => {
if (err) next(err);
res.json(favourites);
});
});
app.post("/api/favourites", (req, res, next) => {
console.log("hello");
const favouritesCollection = db.collection("favourites");
console.log(req.body);
const newFavourite = req.body;
favouritesCollection.insert(newFavourite, (err, result) => {
if (err) next(err);
res.status(201);
// res.json(result.ops[0]);
});
});
app.listen(port, () => {
console.log("App listening on port", port);
});
}
);