Middleware Idea
Middleware is a chain of functions where each can change the request or stop it. Here is the idea in plain JavaScript.
Example
const chain = [(ctx, next) => { ctx.log = ["a"]; next(); }, (ctx, next) => { ctx.log.push("b"); next(); }, (ctx) => ctx.log.push("handler")];
const ctx = {};
let i = 0;
const next = () => chain[i++](ctx, next);
next();
console.log(ctx.log);Result
[ 'a', 'b', 'handler' ]
Exercise
Continue the chain.
()Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.