-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 1.45 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
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
const marvelData = require("./data/marvel.json");
const piratesData = require("./data/pirates.json");
const stwData = require("./data/starwars.json");
const hpData = require("./data/hp.json");
const strangerData = require("./data/strangerthings.json");
const authors = require("./data/authors.json");
const totalData = require("./data/total.json");
getCharacterData = (req, res, data) => {
const filters = req.query;
const filteredData = data.filter((item) => {
let isValid = true;
for (key in filters) {
isValid = isValid && item[key] === filters[key];
}
return isValid;
});
res.json(filteredData);
};
app.get("/", (req, res) => {
res.sendFile("index.html", { root: __dirname });
});
app.get("/api/v1/marvel", (req, res) => {
getCharacterData(req, res, marvelData);
});
app.get("/api/v1/all", (req, res) => {
getCharacterData(req, res, totalData);
});
app.get("/api/v1/pirates", (req, res) => {
getCharacterData(req, res, piratesData);
});
app.get("/api/v1/starwars", (req, res) => {
getCharacterData(req, res, stwData);
});
app.get("/api/v1/hp", (req, res) => {
getCharacterData(req, res, hpData);
});
app.get("/api/v1/strangerthings", (req, res) => {
res.send(strangerData);
});
app.get("/api/authors", (req, res) => {
res.send(authors);
});
app.listen(port, () => {
console.log("app is listening to port " + port);
});