Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Separate routes, controllers, and models for maintainability.
MVC Pattern in Express 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 MVC Pattern in Express example in a small scratch file, then explain what changed and why.