Build HTTP APIs with routing, middleware, validation, controllers, and error handling.
Lessons
Write clean asynchronous handlers without callback nesting.
Async/Await in Routes is easiest to learn by reading the example, changing it, and observing the result.
import express from 'express';
const app = express();
// Wrapper forwards rejected promises to the error handler
const asyncHandler = fn => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next);
app.get('/api/lessons', asyncHandler(async (req, res) => {
const lessons = await db.lessons.findMany();
res.json(lessons);
}));
app.use((err, req, res, next) => res.status(500).json({ message: err.message }));
app.listen(3000);Practice the Async/Await in Routes example in a small scratch file, then explain what changed and why.