forked from fdnd-task/i-love-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (53 loc) · 2.1 KB
/
server.js
File metadata and controls
75 lines (53 loc) · 2.1 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
69
70
71
72
73
74
import express from 'express'
import { Liquid, Value } from 'liquidjs';
import { readdir, readFile } from 'node:fs/promises'
const app = express()
app.set('views', './views')
app.use(express.static('assets'))
const engine = new Liquid();
app.engine('liquid', engine.express());
// ----Hier is de homepage---
app.get('/', async function(request, response){
response.render('index.liquid')
})
// ---MARK: Hier is de warhammer pagina---
app.get('/warhammer', async function (request, response) {
response.render('warhammer.liquid')
})
// ----MARK: Hier is de Spells pagina---
const api = 'https://www.dnd5eapi.co/api/'
const api_spells = "https://www.dnd5eapi.co/api/spells/"
// hier maak ik informatie uit de spells API
const spellsResponse = await fetch(api_spells);
// nu maak ik er een JSON object van
const spellsResponseJSON = await spellsResponse.json();
// console.log(spellsResponseJSON);
// Hier zet ik de data van de spells naar de pagina
app.get('/spells', async function (request, response) {
// hier komt de spellfilter
let spellsURL = 'https://www.dnd5eapi.co/api/spells/';
if (request.query.level) {
spellsURL = spellsURL + `?level=` + request.query.level
}
else {
spellsURL = spellsURL + ''
}
// console.log(spellsURL)
const spellsResponse = await fetch(spellsURL);
const spellsResponseJSON = await spellsResponse.json();
// console.log(spellsResponseJSON)
response.render('spells_showcase.liquid', {spells: spellsResponseJSON.results});
// console.log(spellsResponseJSON.results);
})
// MARK: spreuk details
app.get('/spells/:index', async function (request, response) {
const spreukResponse = await fetch(`${api_spells}${request.params.index}`)
const spreukResponseJSON = await spreukResponse.json()
// console.log(spreukResponseJSON)
response.render('spreuk.liquid', {spreuk: spreukResponseJSON})
})
app.use(express.urlencoded({extended: true}))
app.set('port', process.env.PORT || 8000)
app.listen(app.get('port'), function () {
console.log(`Application started on http://localhost:${app.get('port')}`)
})