/ → toont HTML-pagina/data → stuurt JSON-dataproject/
├─ server.js
├─ public/
│ └─ index.html
└─ data/
└─ leerlingen.json
import http from "http"
import fs from "fs"
import path from "path"
const __dirname = import.meta.dirname
http = server maken
fs = bestanden lezen/schrijven
path = veilige padconstructies
const server = http.createServer((req, res) => {
// Hier behandelen we elk verzoek
})
req = request (van de browser)
res = response (ons antwoord)
server.listen(3000, () => {
console.log('Server luistert op http://localhost:3000')
})
Poort 3000 is een veelgebruikte testpoort.
if (req.method === 'GET' && req.url === '/') {
const filePath = path.join(__dirname, 'public', 'index.html')
fs.readFile(filePath, 'utf8', (err, inhoud) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.end(inhoud)
})
}
else if (req.method === 'GET' && req.url === '/data') {
const filePath = path.join(__dirname, 'data', 'leerlingen.json')
fs.readFile(filePath, 'utf8', (err, inhoud) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json; charset=utf-8')
res.end(inhoud)
})
}
else {
res.statusCode = 404
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('404 - Pagina niet gevonden')
}
{
"6apda": {
"Noa": 10,
"Zakir": 12
},
"6inco": {
"Thomas": 99
}
}
<h1>Leerlingen</h1>
<button id="btn">Laad data</button>
<div id="output"></div>
<script>
btn.addEventListener('click', () => {
fetch('/data')
.then(res => res.json())
.then(data => {
console.log(data)
})
.catch(err => {
console.error('Fout bij ophalen:', err)
})
})
</script>
fetch('/data')
.then(res => res.json())
.then(data => {
for (const klas in data) {
const sectie = document.createElement('section')
const titel = document.createElement('h2')
titel.textContent = klas
sectie.appendChild(titel)
document.body.appendChild(sectie)
}
})
.catch(err => console.error(err))
fetch('/data')
.then(res => res.json())
.then(data => {
for (const klas in data) {
const sectie = document.createElement('section')
const titel = document.createElement('h2')
titel.textContent = klas
sectie.appendChild(titel)
const lijst = document.createElement('ul')
for (const naam in data[klas]) {
const li = document.createElement('li')
li.textContent = `${naam} — ${data[klas][naam]}`
lijst.appendChild(li)
}
sectie.appendChild(lijst)
document.body.appendChild(sectie)
}
})
.catch(err => console.error('Fout:', err))
node server.jshttp://localhost:3000/ → HTML wordt geladenfetch('/data') → Promise.then() verwerken we het antwoordEen Promise is een “belofte” dat er later een resultaat zal zijn.
.then() → wat te doen als het lukt.catch() → wat te doen als er iets fout gaatfetch() stuurt een HTTP-verzoek.then(res => res.json()) zet JSON om naar object.then(data => ...) gebruikt de data.catch() vangt fouten opfetch('/data')
.then(res => res.json())
.then(data => toonData(data))
.catch(fout => console.error(fout))
toonDatafunction toonData(data) {
for (const klas in data) {
const sectie = document.createElement('section')
const titel = document.createElement('h2')
titel.textContent = klas
sectie.appendChild(titel)
const lijst = document.createElement('ul')
for (const naam in data[klas]) {
const li = document.createElement('li')
li.textContent = `${naam} — ${data[klas][naam]}`
lijst.appendChild(li)
}
sectie.appendChild(lijst)
document.body.appendChild(sectie)
}
}
De fetch()-keten is kort, en de logica zit in een aparte functie.
fetch() → Promise
│
├── .then() → OK
│
└── .catch() → Fout
Open DevTools (F12 → tab “Network”).
Bekijk het verzoek naar /data en het antwoord (JSON).