Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Make retries safe with idempotent PUT and DELETE handlers.
Idempotency and Safe Methods 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 Idempotency and Safe Methods example in a small scratch file, then explain what changed and why.