-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (46 loc) · 1.28 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
const PORT = process.env.PORT || 3000;
const express = require("express");
const data = require("./desserts.json");
const app = express();
app.use(function (req, res, next) {
res.header("Allow", "GET");
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.get("/", (req, res) => {
res.json("An Api for desserts.");
});
app.get("/desserts", (req, res) => {
let getJson;
//GET DEESERTS BY CATEGORY//
if (req.query.category) {
let dessertCategory = data.desserts.filter(
(dessert) => dessert.category.toLowerCase() == req.query.category
);
getJson = dessertCategory;
return res.json(getJson);
}
//GET DEESERTS BY TITLE
else if (req.query.title) {
let dessertTitle = data.desserts.filter(
(dessert) => dessert.title.toLowerCase() == req.query.title
);
getJson = dessertTitle;
return res.json(getJson);
}
//GET ALL DESERTS
else {
return res.json(data.desserts);
}
});
//GET DESSERTS BY ID//
app.get(`/desserts/:id`, (req, res) => {
const id = req.params.id;
const dessertId = data.desserts.filter((dessert) => dessert.id == id);
res.json(dessertId);
});
app.listen(PORT, () => console.log(`Port: ${PORT}`));