-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpress.js
47 lines (39 loc) · 1.19 KB
/
express.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
const express = require("express");
const { getJsonData, addJsonData } = require("./supabase.js");
const cors = require("cors");
const app = express();
app.use(cors());
app.use(express.static("assets"));
app.get("/", function (request, response) {
response.sendFile(__dirname + "/index.html");
});
app.get("/get", function (request, response) {
return response.status(400).json({ message: "empty id" });
});
app.get("/get/:id", function (request, response) {
const id = request.params.id;
if (id.length !== 6) {
return response.status(400).json({ message: "incorrect id" });
} else {
getJsonData(id)
.then(function (data) {
response.json(data);
})
.catch((error) => {
return response.status(404).json({ message: error.message });
});
}
});
app.post("/generate", express.json(), function (request, response) {
if (!request.body) {
return response.status(400).json({ message: "no body" });
}
addJsonData(request.body)
.then(function (data) {
response.json(data);
})
.catch((error) => {
return response.status(400).json({ message: error.message });
});
});
app.listen(3000, () => console.log("Server run..."));