layer method

void layer(
  1. Object middleware
)

Wraps everything below this router in middleware.

Takes a shelf Middleware or a const-expressible Layer. Layers run outermost first, in the order they were added.

"Everything below" means everything this router answers, the 404s and 405s included. On a nested router that is scoped to its prefix: a layer on a router mounted at /admin runs for /admin/typo as well as /admin/orders, and never for /health. A merged router has no prefix, so its layers cover the whole application.

That is what lets a layer run before routing, which anything rewriting a path depends on — NormalizePath most of all, since its job is to make a path match. Use routeLayer for the other intent: wrapping only the requests a route actually claimed.

Implementation

void layer(Object middleware) {
  _requireOpen();
  internals.middleware.add(middleware);
}