Concept
Middleware runs before or around route handlers to do shared work: logging, authentication, parsing, rate limiting. Each piece does one thing and passes the request along, or ends it with a response.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Middleware doing its job.
app.addHook("onRequest", async (req) => {
req.startedAt = performance.now();
});
app.addHook("onResponse", async (req, reply) => {
req.log.info({ method: req.method, url: req.url, status: reply.statusCode,
ms: Math.round(performance.now() - req.startedAt) });
});
app.decorateRequest("user", null);
app.addHook("preHandler", requireUser); // sets req.user or replies 401Middleware that forgets to call next() or send a response on an error path. The request hangs with no log line, and it only shows up as a timeout in production.
Your roadmap puts these before Middleware, unless you already know them.
Once a session shows you understand Middleware, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on Middleware.