Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Move route logic into focused controller functions.
Controllers is easiest to learn by reading the example, changing it, and observing the result.
// controllers/lessonController.js
export function listLessons(service) {
return async (req, res, next) => {
try {
res.json(await service.getAll());
} catch (error) {
next(error);
}
};
}
// app.js
import express from 'express';
import { listLessons } from './controllers/lessonController.js';
import lessonService from './services/lessonService.js';
const app = express();
app.get('/api/lessons', listLessons(lessonService));
app.listen(3000);Practice the Controllers example in a small scratch file, then explain what changed and why.