addRoute method

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

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

Route<T> addRoute(String method, String path, T handler,
    {Iterable<T> middleware}) {
  middleware ??= <T>[];
  if (_useCache == true) {
    throw StateError('Cannot add routes after caching is enabled.');
  }

  // Check if any mounted routers can match this
  final handlers = <T>[handler];

  if (middleware != null) handlers.insertAll(0, middleware);

  final route = Route<T>(path, method: method, handlers: handlers);
  _routes.add(route);
  return route;
}