You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 14, 2025. It is now read-only.
This repository is archived and no longer actively maintained.
We are no longer accepting issues, feature requests, or pull requests.
For additional support or questions, please visit the Express.js Discussions page.
Routington
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.
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 of nodes.
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 + ')$', 'i'),
so you need to escape your string, ie \\w.
You can always pre-define names or regular expressions before. For example, I can define:
router.define('/page/:id(\\w{3,30})')// later, :id will have the same regexp// so you don't have to repeat yourselfrouter.define('/page/:id/things')
match, unless null, will be an object with the following properties:
param - A list of named parameters, ex, match.param.id === 'taylorswift'.
node - The matched node.
Will always have name.string === ''.
Building a Router on top of Routington
Each URL you define creates a node,
and you are free to do whatever you'd like with each node as long you don't overwrite any prototype properties (basically just define, match, and parse).
Adding any features to routington shouldn't be necessary.
For example, suppose you want to attach callbacks to a node by extending routington:
functiondispatcher(req,res,next){varmatch=router.match(url.parse(req.url).pathname)if(!match)// this is a 404varnode=match.nodevarcallbacks=node[req.method]if(!callbacks)// this is a 405// execute all the callbacks.// async.series won't actually work here,// but you get the point.async.series(callbacks,next)}
Properties attached to the node will be exposed on the match.
For example,
suppose you wanted to label a node: