-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
40 lines (32 loc) · 934 Bytes
/
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
const fs = require("fs");
const path = require("path");
const express = require("express");
const app = express();
const port = 3000;
const requestDataFromFile = (result, filepath) => {
const file = path.join(__dirname, filepath);
if (fs.existsSync(file)) {
result.json(JSON.parse(fs.readFileSync(file, "utf8")));
} else {
result.status(404).json({
error: {
status: 404,
message: "Not Found",
},
});
}
};
app.use(express.static("dist"));
app.use("/static", express.static("static"));
app.get("/api/device/list", (_, result) => {
requestDataFromFile(result, "data/list.json");
});
app.get("/api/device/:id", (request, result) => {
setTimeout(() => {
requestDataFromFile(result, `data/${request.params.id}.json`);
}, Math.random() * 1000);
});
app.listen(port, () =>
/* eslint-disable no-console */
console.log(`Sample App listening at http://localhost:${port}`)
);