map method
Maps the given URI to the given handler.
uri
- a pattern, either String or RegExp. If String, an instance ofRegExp('^$pattern\$')
will be generated and used for matching the requested URI.
You can also specify the HTTP method in the pattern,
such as GET, POST, and PUT.
For example, 'get:/foo'
accepts only the GET method.
Note: you can specify WebSocket by prefixing with ws:
,
e.g., ws:/mine
.
You can use the named capturing group, see handler
for details.
handler
- the handler for handling the request, or another URI that this request will be forwarded to.
If the handler is a string, you can refer the named capturing
group with (the_group_name)
.
For example: '/dead-link/(?<info>.*)': '/new-link/(info)'
will forward /dead-link/whatevr
to `/new-link/whatever.
If you'd like to redirect, you can do:
'/dead-link/(?<info>.*)': (connect) {
connect.redirect("/new-link/${DefaultRouter.getNamedGroup(connect, 'info')");
}
Note: getNamedGroup is the util you can use in your handler to retrieve the named capturing group.
preceding
- whether to make the mapping preceding any previous mappings. In other words, if true, this mapping will be interpreted first.
Implementation
@override
void map(Pattern uri, Object? handler, {preceding = false}) {
if (handler != null && handler is! Function && handler is! String)
throw ServerError("URI mapping: function (renderer) or string (URI) is required for $uri");
_map(_uriMapping, uri, handler, preceding);
_uriCache.reset();
}