addRoute method

  1. @override
Route<RequestHandler> addRoute (
  1. String method,
  2. String path,
  3. RequestHandler handler,
  4. {Iterable<RequestHandler> middleware}
)
override

Adds a route that responds to the given path for requests with the given method (case-insensitive). Provide '*' as the method to respond to all methods.

Implementation

@override
Route<RequestHandler> addRoute(
    String method, String path, RequestHandler handler,
    {Iterable<RequestHandler> middleware}) {
  middleware ??= [];
  final handlers = <RequestHandler>[];
  // Merge @Middleware declaration, if any
  var reflector = _container?.reflector;
  if (reflector != null && reflector is! ThrowingReflector) {
    Middleware middlewareDeclaration =
        getAnnotation<Middleware>(handler, _container?.reflector);
    if (middlewareDeclaration != null) {
      handlers.addAll(middlewareDeclaration.handlers);
    }
  }

  final handlerSequence = <RequestHandler>[];
  handlerSequence.addAll(middleware ?? []);
  handlerSequence.addAll(handlers);

  return super.addRoute(method, path.toString(), handler,
      middleware: handlerSequence);
}