addRoute method

void addRoute(
  1. Route route, [
  2. String path = '/'
])

Adds route to the server at path.

The path supports:

  • Literal segments: /admin/profile/edit
  • Parameters: /users/:id/posts
  • Wildcards: /api/*/list (single segment)
  • Tails: /files/** (tail match)

The full route path combines path with Route.path. For example, a route with path: '/edit' added via addRoute(route, '/users') handles requests to /users/edit.

Virtual Host Routing

Routes can optionally specify a Route.host to restrict them to specific virtual hosts. When Route.host is null (the default), the route matches any host. When set, the route only matches requests with that specific Host header value.

Tail paths

A tail path (/**) matches all remaining segments and must be terminal. Routes can only be added at a tail path if their Route.injectIn registers a single route at /. This prevents nested tails (e.g., /a/**/b/**), which are invalid.

The default Route.injectIn satisfies this requirement.

Implementation

void addRoute(Route route, [String path = '/']) {
  final hostPrefix = route.host ?? '*';
  final fullPath = '$hostPrefix/$path'; // normalized by relic
  _app.injectAt(fullPath, route);
}