mount static method

void mount(
  1. String prefix,
  2. RawRequestHandler handler, {
  3. bool middleware = false,
})

send every request under prefix straight to handler, before routing and body reading. the handler writes and closes the response itself. Socket.IO uses this for /socket.io/.

The prefix matches whole path parts. /admin and /admin/ both match /admin, /admin/ and /admin/users, never /administrator. The query string is not part of the match.

By default no middleware runs, also no global middleware (security headers, CSRF, rate limit). With middleware set to true, the AppConfig.globalMiddleware runs first. A middleware that gives a response stops the request before handler. The body is not read for the middleware, so the handler can still read it. A terminate step runs once the answer is sent. The global CORS config does not apply to a mount. RequestTimeout does not run for a mount: the handler already owns the answer. Cookies that a middleware adds after the handler (the session cookie, XSRF-TOKEN) are not sent. Get the CSRF token from a normal route.

When two prefixes match, the longest one wins. // counts as one /. The match uses the raw path, so /admin/secure%2Fx goes to a mount at /admin. To protect a nested path, set middleware on the outer mount too.

Route.mount('/socket.io/', io.handle);
Route.mount('/admin', admin.handle, middleware: true);

Implementation

static void mount(
  String prefix,
  RawRequestHandler handler, {
  bool middleware = false,
}) {
  _mounts[prefix] = handler;
  if (middleware) {
    _mountsWithMiddleware.add(prefix);
  } else {
    _mountsWithMiddleware.remove(prefix);
  }
}