route
0.4.6

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:

dependencies:
route: any

Then, run pub install 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.

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
it's 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):

libraryclient;import'package:route/client.dart';main(){varrouter=newRouter()..addHandler(homeUrl,showHome)..addHandler(articleUrl,showArticle)..listen();}voidshowHome(Stringpath){// nothing to parse from path, since there are no groups}voidshowArticle(Stringpath){vararticleId=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.