Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Keep business logic in services apart from HTTP concerns.
Service Layer 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 Service Layer example in a small scratch file, then explain what changed and why.