Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Pass dependencies in so modules stay testable and swappable.
Dependency Injection 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 Dependency Injection example in a small scratch file, then explain what changed and why.