README
@jccr/routington
This is fork of the original Routington with updated dependencies.
Routington is a trie-based URL router. Its goal is only to define and match URLs. It does not handle methods, headers, controllers, views, etc., in anyway. It is faster than traditional, linear, regular expression-matching routers, although insignficantly, and scales with the number of routes.
The purpose of this router isn't for performance, but to bring more structure to URL routing. The intention is for you to build a framework on top either in node.js or in the browser.
Implementations:
- koa-trie-router - for koa
- wayfarer
API
node Node = Routington()
var routington = require('routington')
var router = routington()
router
is the root Node
in the trie. All node
s will have router
as furthest ancestor.
Node
Every node on a tree is an instance of Node
. You only construct the root. A node
has the following properties:
child {}Node
- String based child definitions. For example,node.child['post']
will return a child node withnode.string === 'post'
children []Node
- Name/regex based child definitionsparent Node
- The parent of the nodename
- Name of the node (for parameter matching)string
- String to match the URL fragmentregex
- Regular expression to match the URL fragment
nodes []Node = router.define(route)
var nodes = router.define('/:identity(page|petition)/:id([0-9a-f]{24})')
route
is a definition of a route and is an extension of Express' routing syntax.route
, however, can only be a string.nodes
is an array ofnode
s.
Each fragment of the route, delimited by a /
, can have the following signature:
string
- ex/post
string|string
-|
separated strings, ex/post|page
:name
- Wildcard route matched to a name(regex)
- A regular expression match without saving the parameter (not recommended):name(regex)
- Named regular expression match
You should always name your regular expressions otherwise you can't use the captured value.
The regular expression is built using new RegExp('^(' + regex + ')