angel_route

Pubbuild status

A powerful, isomorphic routing library for Dart.

This API is a huge improvement over the original Angelrouting system, and thus deserves to be its own individual project.

angel_route exposes a routing system that takes the shape of a tree. This tree structurecan be easily navigated, in a fashion somewhat similar to a filesystem. The Router APIis a very straightforward interface that allows for your code to take a shape similar tothe route tree. Users of Laravel and Express will be very happy.

angel_route does not require the use of Angel,and has minimal dependencies. Thus, it can be used in any application, regardless offramework. This includes Web apps, Flutter apps, CLI apps, and smaller servers which donot need all the features of the Angel framework.

Contents

Examples

Routing

If you use Angel, every Angel instance isa Router in itself.

  1. main() {
  2. final router = Router();
  3.  
  4. router.get('/users', () {});
  5.  
  6. router.post('/users/:id/timeline', (String id) {});
  7.  
  8. router.get('/square_root/:id([0-9]+)', (n) {
  9. return { 'result': pow(int.parse(n), 0.5) };
  10. });
  11.  
  12. // You can also have parameters auto-parsed.
  13. //
  14. // Supports int, double, and num.
  15. router.get('/square_root/int:id([0-9]+)', (int n) {
  16. return { 'result': pow(n, 0.5) };
  17. });
  18.  
  19. router.group('/show/:id', (router) {
  20. router.get('/reviews', (id) {
  21. return someQuery(id).reviews;
  22. });
  23.  
  24. // Optionally restrict params to a RegExp
  25. router.get('/reviews/:reviewId([A-Za-z0-9_]+)', (id, reviewId) {
  26. return someQuery(id).reviews.firstWhere(
  27. (r) => r.id == reviewId);
  28. });
  29. }, middleware: [put, middleware, here]);
  30.  
  31. // Grouping can also take async callbacks.
  32. await router.groupAsync('/hello', (router) async {
  33. var name = await getNameFromFileSystem();
  34. router.get(name, (req, res) => '...');
  35. });
  36. }

The default Router does not give any notification of routes being changed, becausethere is no inherent stream of URL's for it to listen to. This is good, because a serverneeds a lot of flexibility with which to handle requests.

Hierarchy

  1. main() {
  2. final router = Router();
  3.  
  4. router
  5. .chain('middleware1')
  6. .chain('other_middleware')
  7. .get('/hello', () {
  8. print('world');
  9. });
  10.  
  11. router.group('/user/:id', (router) {
  12. router.get('/balance', (id) async {
  13. final user = await someQuery(id);
  14. return user.balance;
  15. });
  16. });
  17. }

See the tests for good examples.

In the Browser

Supports both hashed routes and pushState. The BrowserRouter interface exposesa Stream<RoutingResult> onRoute, which can be listened to for changes. It will fire nullwhenever no route is matched.

angel_route will also automatically intercept <a> elements and redirect them toyour routes.

To prevent this for a given anchor, do any of the following:

  • Do not provide an href
  • Provide a download or target attribute on the element
  • Set rel="external"

Route State

  1. main() {
  2. final router = BrowserRouter();
  3. // ..
  4. router.onRoute.listen((route) {
  5. if (route == null)
  6. throw 404;
  7. else route.state['foo'] = 'bar';
  8. });
  9.  
  10. router.listen(); // Start listening
  11. }

For applications where you need to access a chain of handlers, consider usingonResolve instead. You can see an example in web/shared/basic.dart.

Route Parameters

Routes can have parameters, as seen in the above examples.Use allParamsin a RoutingResult to get them as a nice Map:

  1. var router = Router();
  2. router.get('/book/:id/authors', () => ...);
  3.  
  4. var result = router.resolve('/book/foo/authors');
  5. var params = result.allParams; // {'id': 'foo'};