-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (49 loc) · 1.9 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
62
63
64
65
const express = require("express");
const userHandlers = require("./scripts/request_handlers/user-handlers.js");
const strikeHandlers = require("./scripts/request_handlers/strike-handlers.js");
var mustacheExpress = require("mustache-express");
const http = require("http")
const app = express();
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.engine("mustache", mustacheExpress());
app.set("view engine", "mustache");
app.set("views", __dirname + "/public");
/* PAGES */
app.get("/", (request, response) => {
http.get("http://localhost:8081/api/strike", (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk
})
res.on("end", () => {
var dataFormatted = JSON.parse(data).greves.map((greve) => {
start_dateFormatted = new Date(greve.start_date).toLocaleDateString().split("/")
end_dateFormatted = new Date(greve.end_date).toLocaleDateString().split("/")
return ({...greve,
start_date: `${start_dateFormatted[0]}/${start_dateFormatted[1]}/${start_dateFormatted[2]}`,
end_date: `${end_dateFormatted[0]}/${end_dateFormatted[1]}/${end_dateFormatted[2]}`}
)})
response.render("index", { greves: dataFormatted });
})
})
});
app.get("/login", (request, response) => {
response.render("login")
})
app.get("/registo", (request, response)=>{
response.render("register")
})
/*USERS*/
app.post("/api/user", userHandlers.signup);
app.post("/api/login", userHandlers.login);
/*STRIKES*/
app.get("/api/strike", strikeHandlers.getStrikes);
app.get("/api/strike/:id", strikeHandlers.getStrikeById);
app.post("/api/strike", strikeHandlers.insertStrike);
app.put("/api/strike/:id", strikeHandlers.updateStrike);
app.delete("/api/strike/:id", strikeHandlers.deleteStrike);
app.listen(8081, () => {
console.log("Server running at http://localhost:8081");
});