-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (82 loc) · 3.05 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import express from "express";
// Link naar de api van vini mini
const url = "https://api.vinimini.fdnd.nl/api/v1";
const data = await fetch(url)
.then((response) => response.json())
.catch((error) => error);
// Maak een nieuwe express server
const server = express();
// Stel in hoe we express gebruiken
server.set("view engine", "ejs");
server.set("views", "./views");
server.use(express.static("public"));
// Maak een route naar de index.ejs voor categorieën en voor notities
server.get("/", (request, response) => {
let categoriesUrl = url + "/categories";
// let productenUrl = url + "/producten";
fetchJson(categoriesUrl).then((data1) => {
const baseUrl = "https://api.vinimini.fdnd.nl/api/v1/";
const pepijnId = "notities?id=clemozv3c3eod0bunahh71sx7";
const url = `${baseUrl}${pepijnId}`;
fetchJson(url).then((data2) => {
response.render("index", { categories: data1.categories, notities: data2.notities });
});
});
});
// Maak een route naar de ei.ejs
server.get("/ei", async (request, response) => {
let productenUrl = url + "/producten";
await fetchJson(productenUrl).then((data) => {
response.render("producten", { producten: data.producten });
});
});
// Maak een route naar de pinda.ejs
server.get("/pinda", async (request, response) => {
let productenUrl = url + "/producten";
await fetchJson(productenUrl).then((data) => {
response.render("pinda", { data });
});
});
// FORMULIER NOTITIE
// Stel de afhandeling van formulieren in
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
server.post("/", function (req, res, next) {
const baseurl = "https://api.vinimini.fdnd.nl/api/v1/";
const url = `${baseurl}`;
req.body.afgerond = false;
req.body.persoonId = "clemozv3c3eod0bunahh71sx7";
req.body.datum = req.body.datum + ":00Z";
req.body.herinnering = [req.body.herinnering + ":00Z"];
console.log(req.body);
postJson(url + "/notities", req.body).then((data) => {
console.log(JSON.stringify(data));
let newNotitie = { ...req.body };
if (data.success) {
res.redirect("/index.ejs");
} else {
const errormessage = `${data.message}: Mogelijk komt dit door de slug die al bestaat.`;
const newdata = { error: errormessage, values: newData };
res.render("index", newdata);
}
});
});
// Stel het poortnummer in en start express
server.set("port", process.env.PORT || 8000);
server.listen(server.get("port"), function () {
console.log(`serverlication started on http://localhost:${server.get("port")}`);
});
async function fetchJson(url) {
return await fetch(url)
.then((response) => response.json())
.catch((error) => error);
}
export async function postJson(url, body) {
return await fetch(url, {
method: "post",
body: JSON.stringify(body),
headers: { "Content-Type": "application/json" },
})
.then((response) => response.json())
.catch((error) => error);
}