Lesson 23 — Middleware compose
A chain of handlers
In Express and Koa, a request passes through middleware: small functions that each do one job (log, check login, parse JSON) and then call next() to hand over to the next one. Code after next() runs on the way back, like layers of an onion.
compose turns a list of middleware into one function. dispatch(i) runs middleware number i and gives it a next that calls dispatch(i + 1).
Note: this playground runs JavaScript in your browser, not in real Node, so Node-only tools (like fs or http) are not available. The exercise is a small plain-JS simulation of the Node idea.
Why it matters: this is how almost every Node web framework structures a request.
Finish dispatch(): if middleware i exists, call it with ctx and a next function that runs middleware i + 1.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Node.js Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Node.js Foundations books — check the publication year so the examples match current versions.
Search Amazon →