Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Design predictable resources, routes, request bodies, and response shapes.
REST API Design 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 REST API Design example in a small scratch file, then explain what changed and why.