-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
121 lines (110 loc) · 2.97 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require("express");
const app = express();
const elements = require("./data.json").elements;
const _ = require("lodash");
const PORT = process.env.PORT || 8000;
Array.prototype.paginate = function (page = 1, limit = this.length) {
page = Number(page);
limit = Number(limit);
page--;
if (
this.slice(page * limit, page * limit + limit).length > 0 ||
this.length > 0
)
return this.slice(page * limit, page * limit + limit);
else
return {
error: "not found.",
};
};
function queryParams(params, array = elements) {
if (params["filter"]) {
_.forEach(params["filter"], function (key, value) {
array = array.filter((element) => element[value] == key);
});
}
if (params["sort_by"]) _.sortBy(array, [array.sort_by, "number"]);
if (params["reverse"] == "true") _.reverse(array);
return array.paginate(params.page, params.limit);
}
// cor policy
app.use((req, res, next) => {
res.header({"Access-Control-Allow-Origin": "*"});
next();
})
app.get("/", async (req, res) => {
res.json(queryParams(req.query));
});
app.get("/period/:period", (req, res) => {
res.json(
queryParams(
req.query,
elements.filter((element) => element.period == req.params.period)
)
);
});
app.get("/random", (req, res) => {
res.json(elements[Math.floor(Math.random() * elements.length - 1)]);
});
app.get("/phase/:phase", (req, res) => {
res.json(
queryParams(
req.query,
elements.filter((element) => element.phase == req.params.phase)
)
);
});
app.get("/name/:name", (req, res) => {
res.json(
elements.filter((element) => element.name == req.params.name).paginate(1, 1)
);
});
app.get("/number/:number", (req, res) => {
res.json(
elements
.filter((element) => element.number == req.params.number)
.paginate(1, 1)
);
});
app.get("/symbol/:symbol", (req, res) => {
res.json(
elements
.filter((element) => element.symbol == req.params.symbol)
.paginate(1, 1)
);
});
app.get("/category/:name", (req, res) => {
res.json(
elements
.filter((element) => element.category == req.params.name)
.paginate(1, 1)
);
});
app.get("/spectral/:element", (req, res) => {
if (
req.params.element.length <= 2 &&
elements.find((element) => element.symbol == req.params.element)
.spectral_img != null
) {
res.redirect(
elements.find((element) => element.symbol == req.params.element)
.spectral_img
);
} else if (
req.params.element.length > 3 &&
elements.find((element) => element.name == req.params.element)
.spectral_img != null
) {
res.redirect(
elements.find((element) => element.name == req.params.element)
.spectral_img
);
} else {
res.json({
error: "not found",
});
}
});
app.listen(PORT, async () => {
console.log("App listening at localhost:8000");
});