-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssr-server.js
83 lines (75 loc) · 1.97 KB
/
ssr-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
require("dotenv").config();
const lowDb = require("lowdb");
const express = require("express");
const FileSync = require("lowdb/adapters/FileSync");
// const rateLimit = require("express-rate-limit");
const jwt = require("express-jwt");
const db = lowDb(new FileSync("db/db.json"));
const app = express();
//Configs
// const limiter = rateLimit({
// windowMs: 5 * 60 * 1000, // 5 minutes
// max: 100, // limit each IP to 100 requests per windowMs
// message: `Too many requests, please try again later.`,
// });
////
//ENV variables
const {
env: { PORT, SECRET_KEY },
} = process;
////
// const token = jwt2.sign({ payload: "test" }, SECRET_KEY); // this is how generate token with jsonwebtoken to consume API
// console.log(token);
//Modules
// app.use(limiter);
app.use(express.json());
app.use(jwt({ secret: SECRET_KEY, algorithms: ["HS256"] }));
app.use(function (req, res, next) {
if (req.method === "OPTIONS") {
res.status(200);
}
next();
});
////
//**** Http Requests *****//
//Get all
app.get("/messi/all", (request, response) => {
const data = db.get("messi");
response.send(data);
});
//Get goal by id
app.get("/messi/getId/:id", (request, response) => {
const data = db
.get("messi")
.find({ id: parseInt(request.params.id) })
.value();
response.send(data);
});
//Get lastest without recieved/shot position
app.get("/messi/latest-to-fill", (request, response) => {
const data = db
.get("messi")
.filter((item) => {
return !item.shot || !item.received;
})
.take()
.value();
response.send(data[0]);
});
//update or create shot and received
app.post("/messi/update/:id", (request, response) => {
const {
body: { shot, received },
params: { id },
} = request;
db.get("messi")
.find({ id: parseInt(id) })
.update("shot", () => shot)
.update("received", () => received)
.write();
return response.send({ success: true });
});
//Listener
app.listen(PORT, () => {
console.log(`Your port is ${PORT}`);
});