-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
index.js
61 lines (57 loc) · 1.23 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
60
61
const express = require("express");
const helper = require("./helper");
const app = express();
const port = 3000;
app.use(express.json());
app.get("/", (req, res) => {
const jsonlist = helper.getDirList();
const result = jsonlist.map((item) => {
return {
filename: item,
url: helper.getUrl(item),
};
});
res.json({
status: true,
result,
});
});
app.get("/:id", (req, res) => {
try {
const id = req?.params?.id;
if (id == null) {
throw new Error(":id must not be empty");
}
res.json(helper.getJsonContent(id));
} catch (error) {
res.status(500).json({
status: false,
error: error.message,
});
}
});
app.post("/save", (req, res) => {
try {
const body = req.body;
const name = body?.name;
const data = body?.data;
if (name == null || data == null) {
throw new Error(
"name (string) or data (json) must be on the request body"
);
}
const result = helper.save(name, data);
res.json({
status: true,
result,
});
} catch (error) {
res.status(500).json({
status: false,
error: error.message,
});
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});