Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Hide persistence behind a repository so routes stay clean.
Repository Pattern 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 Repository Pattern example in a small scratch file, then explain what changed and why.