Router class

A request router that makes it easier to handle HttpRequests from an HttpServer.

serve creates a new Stream of requests whose paths match against the given pattern. Matching requests are not sent to any other streams created by a server() call.

filter registers a Filter function to run against matching requests. On each request the filters that match are run in order, waiting for each to complete since filters return a Future. If any filter completes false, the subsequent filters and request handlers are not run. This way a filter can prevent further processing, like needed for authentication.

Requests not matched by a call to serve are sent to the defaultStream. If there's no subscriber to the defaultStream then a 404 is sent to the response.

Example: import 'package:route/server.dart'; import 'package:route/pattern.dart';

HttpServer.bind().then((server) {
  var router = new Router(server)
    ..filter(matchesAny(['/foo', '/bar']), authFilter)
    ..serve('/foo').listen(fooHandler)
    ..serve('/bar').listen(barHandler)
    ..defaultStream.listen(send404);
});

Constructors

Router(Stream<HttpRequest> incoming)
Create a new Router that listens to the incoming stream, usually an instance of HttpServer.

Properties

defaultStream Stream<HttpRequest>
read-only
hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

filter(Pattern url, Filter filter) → void
A Filter returns a Future<bool> that tells the router whether to apply the remaining filters and send requests to the streams created by serve. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
serve(Pattern url, {String method}) Stream<HttpRequest>
Request whose URI matches url and method (if provided) are sent to the stream created by this method, and not sent to any other router streams.
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator. [...]
inherited