Router class Null safety

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 = request.params['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 {
  assert(userName == request.params['userName']);
  return Response.ok('Hello ${userName}');
});

// 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(request.params['msgId']!);
  return Response.ok(message.getById(msgId));
});

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

If multiple routes match the same request, the handler for the first route is called. If no route matches a request, a Response.notFound will be returned instead. The default matcher can be overridden with the notFoundHandler constructor parameter.

Annotations

Constructors

Router({Handler notFoundHandler = _defaultNotFound})
Creates a new Router routing requests to handlers.

Properties

hashCode int
The hash code for this object.
read-onlyinherited
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited

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.
call(Request request) Future<Response>
Route incoming requests to registered handlers.
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, Handler handler) → void
Mount a handler 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
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

Static Properties

routeNotFound Response
Sentinel Response object indicating that no matching route was found.
final