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 = 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.
- Available extensions
- Annotations
-
- @sealed
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- plus → RouterPlus
-
Available on Router, provided by the RouterPlusExtension extension
Upgrades the shelf Router to RouterPlusno setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
add(
String verb, String route, Function handler) → void -
Add
handler
forverb
requests toroute
. -
all(
String route, Function handler) → void -
Handle all request to
route
usinghandler
. -
call(
Request request) → Future< Response> - Route incoming requests to registered handlers.
-
connect(
String route, Function handler) → void -
Handle
CONNECT
request toroute
usinghandler
. -
delete(
String route, Function handler) → void -
Handle
DELETE
request toroute
usinghandler
. -
get(
String route, Function handler) → void -
Handle
GET
request toroute
usinghandler
. -
head(
String route, Function handler) → void -
Handle
HEAD
request toroute
usinghandler
. -
mount(
String prefix, Handler handler) → void - Mount a handler below a prefix.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
options(
String route, Function handler) → void -
Handle
OPTIONS
request toroute
usinghandler
. -
patch(
String route, Function handler) → void -
Handle
PATCH
request toroute
usinghandler
. -
post(
String route, Function handler) → void -
Handle
POST
request toroute
usinghandler
. -
put(
String route, Function handler) → void -
Handle
PUT
request toroute
usinghandler
. -
toString(
) → String -
A string representation of this object.
inherited
-
trace(
String route, Function handler) → void -
Handle
TRACE
request toroute
usinghandler
.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- routeNotFound → Response
-
Sentinel Response object indicating that no matching route was found.
final