forked from fdnd-task/user-experience-enhanced-website
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (47 loc) · 1.52 KB
/
Copy pathindex.js
File metadata and controls
68 lines (47 loc) · 1.52 KB
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
import express from 'express'
const url = 'https://api.vinimini.fdnd.nl/api/v1'
// 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'))
// Maak een route voor de index
app.get('/', (request, response) => {
let categoriesUrl = url + '/categories'
fetchJson(categoriesUrl).then((data) => {
response.render('index', data)
})
})
// Route voor de producten
app.get('/categorie', (request, response) => {
let query = request.query.categorieId
let productenUrl = url + `/producten?categorieId=${query}`
fetchJson(productenUrl).then((data) => {
response.render('pinda-ei-producten', {producten: data});
});
})
app.get('/product', (request, response) => {
let query = request.query.id
let productenUrl = url + `/product?id=${query}`
console.log(productenUrl)
fetchJson(productenUrl).then((data) => {
console.log(data)
response.render('pinda-product', 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)
}