roux

Pub Version Test

Lightweight, fast router for Dart with static, parameterized, and wildcard route matching.

Install

dart pub add roux

With Flutter:

flutter pub add roux

Quick Start

import 'package:roux/roux.dart';

final router = createRouter<String>();

addRoute(router, 'GET', '/users', 'users');
addRoute(router, 'GET', '/users/:id', 'user');
addRoute(router, 'POST', '/users', 'create-user');
addRoute(router, null, '/health', 'any-method');

final match = findRoute(router, 'GET', '/users/123');
print(match?.data);   // user
print(match?.params); // {id: 123}

final all = findAllRoutes(router, 'GET', '/users/123');
for (final m in all) {
  print(m.data);
}

Route Syntax

  • Static: /users
  • Named param: /users/:id
  • Embedded params: /files/:name.:ext
  • Single-segment wildcard: * (unnamed, captured as _0, _1, ...)
  • Multi-segment wildcard: ** (unnamed, captured as _) or **:path (named)
  • ** can match an empty remainder; **:name requires at least one segment.
  • Escape literal tokens with \\: /static\\:path/\\*/\\*\\* matches /static%3Apath/*/**
  • Paths are normalized to start with /.
  • Methods are case-insensitive. null uses the any-method token.

Matching Order

  • Static > param > wildcard.
  • Method matching tries the requested method first, then the any-method token.

Options

final router = createRouter<String>(
  caseSensitive: false,
  anyMethodToken: 'any',
);

Examples

Any-method route

addRoute(router, null, '/status', 'ok');

Find all matches

final matches = findAllRoutes(router, 'GET', '/files/report.pdf');
for (final m in matches) {
  print(m.data);
}

Convert a pattern to RegExp

final re = routeToRegExp('/users/:id');

License

MIT. See LICENSE.

Libraries

roux