{"name":"routington","version":"0.1.2","author":{"url":"http://jongleberry.com","name":"Jonathan Ong","email":"me@jongleberry.com"},"license":"MIT","_id":"routington@0.1.2","maintainers":[{"name":"jongleberry","email":"jonathanrichardong@gmail.com"}],"homepage":"https://github.com/jonathanong/routington","bugs":{"url":"https://github.com/jonathanong/routington/issues","email":"me@jongleberry.com"},"dist":{"shasum":"92afd9d7689d760761732d2f252de0a96542126d","tarball":"http://registry-npm.ywwl.com/routington/download/routington-0.1.2.tgz","integrity":"sha512-4ud9y4FIny6EjLQioBBOzZ0uXyQl3iPsCWhP9VGDO3vam2ajIpKjUdD7ZkBSVoabnY4yBNqiuDUa/VSpLdgHYw==","signatures":[{"sig":"MEQCIEvEO2B85RhHqJyaPUOaJAiuzL/BxfM2n1fnsvN2TJL+AiAz77M+Y4RJkC3n3EN4Kj3deWDAbYdhdQd0GCCFN0Zh9w==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"size":5408,"noattachment":false,"key":"/routington/-/routington-0.1.2.tgz"},"_from":".","scripts":{"test":"make test"},"_npmUser":{"name":"jongleberry","email":"jonathanrichardong@gmail.com"},"repository":{"url":"https://github.com/jonathanong/routington.git","type":"git"},"_npmVersion":"1.3.26","description":"Trie-based URL Routing","directories":{},"devDependencies":{"mocha":"*","matcha":"*","should":"*"},"readme":"## Routington\n\n[![NPM version][npm-image]][npm-url]\n[![Build status][travis-image]][travis-url]\n[![Test coverage][coveralls-image]][coveralls-url]\n[![Dependency Status][david-image]][david-url]\n[![License][license-image]][license-url]\n[![Downloads][downloads-image]][downloads-url]\n[![Gittip][gittip-image]][gittip-url]\n\nRoutington is a [trie](http://en.wikipedia.org/wiki/Trie)-based URL router.\nIts goal is only to define and match URLs.\nIt does not handle methods, headers, controllers, views, etc., in anyway.\nIt is faster than traditional, linear, regular expression-matching routers, although insignficantly,\nand scales with the number of routes.\n\nThe purpose of this router isn't for performance,\nbut to bring more structure to URL routing.\nThe intention is for you to build a framework on top either in node.js or in the browser.\n\nImplementations:\n\n  - [koa-trie-router](https://github.com/koajs/trie-router) - for [koa](https://github.com/koajs)\n  - [wayfarer](https://github.com/yoshuawuyts/wayfarer)\n\n### API\n\n#### node Node = Routington()\n\n```js\nvar routington = require('routington')\nvar router = routington()\n```\n\n`router` is the root `Node` in the trie. All `node`s will have `router` as furthest ancestor.\n\n#### Node\n\nEvery node on a tree is an instance of `Node`. You only construct the root. A `node` has the following properties:\n\n- `child {}Node` - String based child definitions.\n  For example, `node.child['post']` will return a child node with `node.string === 'post'`\n- `children []Node` - Name/regex based child definitions\n- `parent Node` - The parent of the node\n- `name` - Name of the node (for parameter matching)\n- `string` - String to match the URL fragment\n- `regex` - Regular expression to match the URL fragment\n\n#### nodes []Node = router.define(route)\n\n```js\nvar nodes = router.define('/:identity(page|petition)/:id([0-9a-f]{24})')\n```\n\n- `route` is a definition of a route and is an extension of Express' routing syntax.\n  `route`, however, can only be a string.\n- `nodes` is an array of `node`s.\n\nEach fragment of the route, delimited by a `/`, can have the following signature:\n\n- `string` - ex `/post`\n- `string|string` - `|` separated strings, ex `/post|page`\n- `:name` - Wildcard route matched to a name\n- `(regex)` - A regular expression match without saving the parameter (not recommended)\n- `:name(regex)`- Named regular expression match\n\nYou should always name your regular expressions otherwise you can't use the captured value.\nThe regular expression is built using `new RegExp('^(' + regex + ')$', 'i')`,\nso you need to escape your string, ie `\\\\w`.\nYou can always pre-define names or regular expressions before. For example, I can define:\n\n```js\nrouter.define('/page/:id(\\\\w{3,30})')\n\n// later, :id will have the same regexp\n// so you don't have to repeat yourself\nrouter.define('/page/:id/things')\n```\n\n#### match {} = router.match(url)\n\n```js\nrouter.define('/page/:id(\\\\w{3,30})')\nvar match = router.match('/page/taylorswift')\n```\n\n`match`, unless `null`, will be an object with the following properties:\n\n- `param` - A list of named parameters, ex, `match.param.id === 'taylorswift'`.\n- `node` - The matched node.\n  Will always have `name.string === ''`.\n\n### Building a Router on top of Routington\n\nEach URL you define creates a node,\nand 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`).\nAdding any features to routington shouldn't be necessary.\n\nFor example, suppose you want to attach callbacks to a node by extending routington:\n\n```js\nrouter.get('/:id/:controller', function (req, res, next) {\n  console.log('do something')\n})\n```\n\nYou can attach the middleware to a `node.GET` array:\n\n```js\nrouter.get = function (path, handler) {\n  var node = router.define(path)[0]\n  node.GET = node.GET || []\n  node.GET.push(handler)\n}\n```\n\nNow, dispatching is easy:\n\n```js\nfunction dispatcher(req, res, next) {\n  var match = router.match(url.parse(req.url).pathname)\n  if (!match)\n    // this is a 404\n\n  var node = match.node\n  var callbacks = node[req.method]\n  if (!callbacks)\n    // this is a 405\n\n  // execute all the callbacks.\n  // async.series won't actually work here,\n  // but you get the point.\n  async.series(callbacks, next)\n}\n```\n\nProperties attached to the node will be exposed on the match.\nFor example,\nsuppose you wanted to label a node:\n\n```js\nvar node = router.define('/:id/:controller')[0]\nnode.label = 'controller'\n```\n\nWhen matched, it will be available via `match.node.label`:\n\n```js\nvar match = router.match('/someid/somecontroller')\nassert(match.node.label === 'label')\n```\n\nSince reaching into `match.node` is a little inconvenient and you probably don't want your end users to touch it,\nyou should expose in your dispatcher:\n\n```js\nvar match = router.match(url.parse(req.url).pathname)\n\n// ...\n\nreq.param = match.param\nreq.label = match.node.label\n```\n\n### Browser Support\n\nIE9+\n\n[npm-image]: https://img.shields.io/npm/v/routington.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/routington\n[github-tag]: http://img.shields.io/github/tag/pillarjs/routington.svg?style=flat-square\n[github-url]: https://github.com/pillarjs/routington/tags\n[travis-image]: https://img.shields.io/travis/pillarjs/routington.svg?style=flat-square\n[travis-url]: https://travis-ci.org/pillarjs/routington\n[coveralls-image]: https://img.shields.io/coveralls/pillarjs/routington.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/pillarjs/routington?branch=master\n[david-image]: http://img.shields.io/david/pillarjs/routington.svg?style=flat-square\n[david-url]: https://david-dm.org/pillarjs/routington\n[license-image]: http://img.shields.io/npm/l/routington.svg?style=flat-square\n[license-url]: LICENSE.md\n[downloads-image]: http://img.shields.io/npm/dm/routington.svg?style=flat-square\n[downloads-url]: https://npmjs.org/package/routington\n[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square\n[gittip-url]: https://www.gittip.com/jonathanong/\n","publish_time":1392237729504,"deprecated":"This package is archived and no longer maintained. For support, visit https://github.com/expressjs/express/discussions","_cnpm_publish_time":1392237729504,"dist-tags":{"latest":"1.0.3"}}