route2 0.1.1

Route #

Route is a client + server routing library for Dart that helps make building single-page web apps and using HttpServer easier.

Installation #

Add this package to your pubspec.yaml file:

  route2: any

Then, run pub get to download and link in the package.

UrlPattern #

Route is built around UrlPattern a class that matches, parses and produces URLs. A UrlPattern is similar to a regex, but constrained so that it is reversible, given a UrlPattern and a set of arguments you can produce the URL that when parsed returns those same arguments. This is important for keeping the URL space for your app flexible so that you can change a URL for a resource in one place and keep your app working.

Route lets you use the same URL patterns for client-side and server-side routing. Just define a library containing all your URLs.

As an example, consider a blog with a home page and an article page. The article URL has the form /article/1234. We want to show articles without reloading the page.

Example (urls.dart):

library urls;

import 'package:route2/url_pattern.dart';

final homeUrl = new UrlPattern(r'/');
final articleUrl = new UrlPattern(r'/article/(\d+)');
final allUrls = [homeUrl, articleUrl];

Client Routing #

On the client, there is a Router class that associates UrlPatterns to handlers. Given a URL, the router finds a pattern that matches, and invokes its handler. This is similar to HttpServer.addRequestHandler(matcher, handler) on the server. The handlers are then responsible for rendering the appropriate changes to the page.

The Router can listen to Window.onPopState events and invoke the correct handler so that the back button seamlessly works.

Example (client.dart):

library client;

import 'package:route2/client.dart';

main() {
  var router = new Router()
      ..addHandler(homeUrl, showHome)
      ..addHandler(articleUrl, showArticle)

void showHome(String path) {
  // nothing to parse from path, since there are no groups

void showArticle(String path) {
  var articleId = articleUrl.parse(req.path)[0];
  // show article page with loading indicator
  // load article from server, then render article

Server Routing #

On the server, route gives you a utility function to match HttpRequests against UrlPatterns.

import 'urls.dart';
import 'package:route2/server.dart';
import 'package:route2/pattern.dart';

HttpServer.bind().then((server) {
  var router = new Router(server)
      ..filter(matchesAny(allUrls), authFilter)
      ..serve(articleUrl, method: 'GET').listen(serveArticle)

Future<bool> authFilter(req) {
  return getUser(getUserIdCookie(req)).then((user) {
    if (user != null) return true;
    return false;

serveArticle(req) {
  var articleId = articleUrl.parse(req.uri.path)[0];
  // retrieve article data and respond

Further Goals #

  • Integration with Polymer so that the changing of UI views can happen automatically.
  • Handling different HTTP methods to help implement REST APIs.
  • Automatic generation of REST URLs from a single URL pattern, similar to Ruby on Rails
  • Helpers for nested views and key-value URL schemes common with complex apps.
  • [Done] Server-side routing for the dart:io v2 HttpServer
  • [Done] IE 9 support

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  route2: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:route2/client.dart';
import 'package:route2/pattern.dart';
import 'package:route2/server.dart';
import 'package:route2/url_pattern.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health issues and suggestions

Fix lib/url_pattern.dart. (-14.49 points)

Analysis of lib/url_pattern.dart reported 1 warning, 21 hints, including:

line 197 col 43: Parameters can't override default values, this method overrides 'Pattern.allMatches' where this positional parameter has a different value.

line 10 col 23: Unnecessary new keyword.

line 12 col 36: Unnecessary new keyword.

line 14 col 1: Prefer using /// for doc comments.

line 93 col 50: Use = to separate a named parameter from its default value.

Fix lib/pattern.dart. (-7.81 points)

Analysis of lib/pattern.dart reported 1 warning, 6 hints, including:

line 23 col 43: Parameters can't override default values, this method overrides 'Pattern.allMatches' where this positional parameter has a different value.

line 5 col 1: Prefer using /// for doc comments.

line 20 col 17: Don't type annotate initializing formals.

line 21 col 8: Don't type annotate initializing formals.

line 47 col 1: Prefer using /// for doc comments.

Fix lib/client.dart. (-6.31 points)

Analysis of lib/client.dart reported 13 hints, including:

line 13 col 17: Unnecessary new keyword.

line 19 col 1: Prefer using /// for doc comments.

line 29 col 3: Prefer using /// for doc comments.

line 36 col 21: Unnecessary new keyword.

line 40 col 3: Prefer using /// for doc comments.

Fix lib/server.dart. (-6.31 points)

Analysis of lib/server.dart reported 13 hints, including:

line 5 col 1: Prefer using /// for doc comments.

line 15 col 8: Unused import: 'url_pattern.dart'.

line 21 col 1: Prefer using /// for doc comments.

line 56 col 41: Unnecessary new keyword.

line 59 col 7: Unnecessary new keyword.

Fix lib/src/async_utils.dart. (-1 points)

Analysis of lib/src/async_utils.dart reported 2 hints:

line 15 col 53: Unnecessary new keyword.

line 16 col 11: Unnecessary new keyword.

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Provide a file named (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

The package description is too short. (-14 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and route2.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
logging >=0.9.0 <2.0.0 0.11.4
Dev dependencies
mockito any
path >=0.9.0 <2.0.0
test ^1.0.0