In Koa,
all middleware are essentially decorators for all following middleware:

  1. app.use(function* decorator(function (subapp) {
  2. // do something before subapp executes
  3. yield* subapp;
  4. // do something after subapp executes
  5. }))
  6. app.use(function* subapp(next) {
  7. this.response.body = 'hello world';
  8. })

For more information on decorators, look up “decorator functions” or the “decorator pattern”.
I would add a link to a good resource, but I don’t think any of the ones
I’ve found yet are that good.

Exercise

Create a middleware that escapes all the HTML entities in the response.