Router class

A shelf Router routes requests to handlers based on HTTP verb and route pattern.

import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;

var app = Router();

// Route pattern parameters can be specified <paramName>
app.get('/users/<userName>/whoami', (Request request) async {
  // The matched values can be read with params(request, param)
  var userName = params(request, 'userName');
  return Response.ok('You are ${userName}');
});

// The matched value can also be taken as parameter, if the handler given
// doesn't implement Handler, it's assumed to take all parameters in the
// order they appear in the route pattern.
app.get('/users/<userName>/say-hello', (Request request, String userName) async {
  return Response.ok('Hello ${uName}');
});

// It is possible to have multiple parameters, and if desired a custom
// regular expression can be specified with <paramName|REGEXP>, where
// REGEXP is a regular expression (leaving out ^ and $).
// If no regular expression is specified `[^/]+` will be used.
app.get('/users/<userName>/messages/<msgId|\d+>', (Request request) async {
  var msgId = int.parse(params(request, 'msgId'));
  return Response.ok(message.getById(msgId));
});

var server = await io.serve(app.handler, 'localhost', 8080);

If multiple routes match the same request, the handler for the first route is called. If the handler returns null the next matching handler will be attempted.

Constructors

Router()

Properties

handler Handler
Get a Handler that will route incoming requests to registered handlers.
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

add(String verb, String route, Function handler) → void
Add handler for verb requests to route. [...]
all(String route, Function handler) → void
Handle all request to route using handler.
connect(String route, Function handler) → void
Handle CONNECT request to route using handler.
delete(String route, Function handler) → void
Handle DELETE request to route using handler.
get(String route, Function handler) → void
Handle GET request to route using handler. [...]
Handle HEAD request to route using handler.
mount(String prefix, Router router) → void
Mount a router below a prefix. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
options(String route, Function handler) → void
Handle OPTIONS request to route using handler.
patch(String route, Function handler) → void
Handle PATCH request to route using handler.
post(String route, Function handler) → void
Handle POST request to route using handler.
put(String route, Function handler) → void
Handle PUT request to route using handler.
toString() String
Returns a string representation of this object.
inherited
trace(String route, Function handler) → void
Handle TRACE request to route using handler.

Operators

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