Catch it and send a 500
Watch the status the client sees for a good request and a failing one.
const http = require('node:http');async function load(url) { if (url === '/boom') throw new Error('db down'); return 'data';}const server = http.createServer(async (req, res) => { try { res.end(await load(req.url)); } catch (err) { console.log('caught: ' + err.message); res.statusCode = 500; res.end('something broke'); }});server.listen(0, async () => { const { port } = server.address(); const ok = await fetch(`http://localhost:${port}/ok`); console.log('/ok → ' + ok.status); const bad = await fetch(`http://localhost:${port}/boom`); console.log('/boom → ' + bad.status); server.close();});1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…