Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Return accurate 2xx, 3xx, 4xx, and 5xx codes for each outcome.
HTTP Status Codes is easiest to learn by reading the example, changing it, and observing the result.
import express from 'express';
const app = express();
app.use(express.json());
const lessons = [];
app.get('/api/v1/lessons', (req, res) => res.json({ data: lessons }));
app.post('/api/v1/lessons', (req, res) => {
const lesson = { id: lessons.length + 1, ...req.body };
lessons.push(lesson);
res.status(201).json({ data: lesson });
});
app.put('/api/v1/lessons/:id', (req, res) => res.json({ data: { id: req.params.id, ...req.body } }));
app.delete('/api/v1/lessons/:id', (req, res) => res.status(204).end());
app.listen(3000);Practice the HTTP Status Codes example in a small scratch file, then explain what changed and why.