forked from fdnd-task/enhanced-website
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index 2.js
66 lines (52 loc) · 1.58 KB
/
index 2.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
import express from 'express'
// const url = "https://zoeken.oba.nl/api/v1/search/";
// Maak een nieuwe express app
const app = express()
// Stel in hoe we express gebruiken
app.set('view engine', 'ejs')
app.set('views', './views')
app.use(express.static('public'))
// Stel de afhandeling van formulieren in
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// Maak een route voor de index pagina
app.get('/', (request, response) => {
fetchJson().then((data) => {
response.render('index', data)
})
})
// Maak een route voor de detail pagina
app.get("/detail", (request, response) => {
fetchJson().then((data) => {
response.render('detail', data)
})
});
app.get('succes', (request, response) => {
fetchJson().then((data) => {
response.render('succes', data)
})
})
// Stel het poortnummer in en start express
app.set('port', process.env.PORT || 8000)
app.listen(app.get('port'), function () {
console.log(`Application started on http://localhost:${app.get('port')}`)
})
/**
* Wraps the fetch api and returns the response body parsed through json
* @param {*} url the api endpoint to address
* @returns the json response from the api endpoint
*/
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)
}