UNPKG

5.41 kBMarkdownView Raw
1# wayfarer [![stability][0]][1]
2[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7]
3[![downloads][8]][9] [![js-standard-style][10]][11]
4
5Composable [trie based](https://en.wikipedia.org/wiki/Trie) router. It's
6faster than traditional, linear, regular expression-matching routers, although
7insignficantly, and scales with the number of routes.
8
9If you're looking for a client-side router check out
10[sheet-router](https://github.com/yoshuawuyts/sheet-router). If you're looking
11for a server router check out
12[server-router](https://github.com/yoshuawuyts/server-router).
13
14### features
15- works with any framework
16- built for speed
17- minimal dependencies
18- extensible
19
20## Installation
21```sh
22$ npm install wayfarer
23```
24
25## Usage
26```js
27var wayfarer = require('wayfarer')
28
29var router = wayfarer('/404')
30
31router.on('/', () => console.log('/'))
32router.on('/404', () => console.log('404 not found'))
33router.on('/:user', (params) => console.log('user is %s', params.user))
34router.on('/wildcard/*', (params) => console.log('wildcard path is %s', params.wildcard))
35
36router('tobi')
37// => 'user is tobi'
38
39router('/uh/oh')
40// => '404 not found'
41
42router('/wildcard/example/path')
43// => 'wildcard path is example/path'
44```
45
46## Subrouting
47Routers can be infinitely nested, allowing routing to be scoped per view.
48Matched params are passed into subrouters. Nested routes will call their
49parent's default handler if no path matches.
50```js
51var r1 = wayfarer()
52var r2 = wayfarer()
53
54r2.on('/child', () => console.log('subrouter trix!'))
55r1.on('/:parent', r2)
56
57r1('/dada/child')
58// => 'subrouter trix!'
59```
60
61## Walk
62Sometimes it's necessary to walk the `trie` to apply transformations.
63```js
64var walk = require('wayfarer/walk')
65var wayfarer = require('wayfarer')
66
67var router = wayfarer()
68router.on('/multiply', (x, y) => x * y)
69router.on('/divide', (x, y) => x / y)
70
71walk(router, (route, cb) => {
72 var y = 2
73 return function (params, x) {
74 return cb(x, y)
75 }
76})
77
78router('/multiply', 4)
79// => 8
80router('/divide', 8)
81// => 4
82```
83
84## API
85### router = wayfarer(default)
86Initialize a router with a default route. Doesn't ignore querystrings and
87hashes.
88
89### router.on(route, cb(params, [arg1, ...]))
90Register a new route. The order in which routes are registered does not matter.
91Routes can register multiple callbacks. The callback can return values when
92called.
93
94### val = router(route, [arg1, ...])
95Match a route and execute the corresponding callback. Alias: `router.emit()`.
96Returns a values from the matched route (e.g. useful for streams). Any
97additional values will be passed to the matched route.
98
99## Internals
100Wayfarer is built on an internal trie structure. If you want to build a router
101using this trie structure it can be accessed through
102`require('wayfarer/trie')`. It exposes the following methods:
103- `trie = Trie()` - create a new trie
104- `node = trie.create(route)` - create a node at a path, and return a node
105- `node = trie.match(route)` - match a route on the the trie and return a node
106- `trie.mount(path, trie)` - mount a trie onto a node at route
107
108## Known issues
109### multiple nested partials don't match
110E.g. `/:foo/:bar/:baz` won't work. This is due `Trie.mount()` overriding child
111partial paths when mounted. I'll get around to fixing this at some point in the
112future, but if anyone wants to contribute a patch it'd most appreciated.
113
114## FAQ
115### Why did you build this?
116Routers like [react-router](https://github.com/rackt/react-router) are
117complicated solutions for a simple problem. In reality all that's needed is a
118[methodless](http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) router
119that can define + match paths and can mount other routers to delegate requests.
120
121### Why isn't my route matching?
122Wayfarer only compares strings. Before you pass in an url you probably want to
123strip it of querystrings and hashes using the
124[pathname-match](https://github.com/yoshuawuyts/pathname-match) module.
125
126## See Also
127- [sheet-router](https://github.com/yoshuawuyts/sheet-router) - fast, modular
128 client-side router
129- [server-router](https://github.com/yoshuawuyts/server-router) - server router
130- [hash-match](https://github.com/sethvincent/hash-match) - easy
131 `window.location.hash` matching
132- [pathname-match](https://github.com/yoshuawuyts/pathname-match) - strip
133 querystrings and hashes from a url
134- [wayfarer-to-server](https://github.com/yoshuawuyts/wayfarer-to-server) -
135 Wrap wayfarer to provide HTTP method matching and `req, res` delegation
136
137## License
138[MIT](https://tldrlegal.com/license/mit-license)
139
140[0]: https://img.shields.io/badge/stability-2%20stable-brightgreen.svg?style=flat-square
141[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
142[2]: https://img.shields.io/npm/v/wayfarer.svg?style=flat-square
143[3]: https://npmjs.org/package/wayfarer
144[4]: https://img.shields.io/travis/yoshuawuyts/wayfarer/master.svg?style=flat-square
145[5]: https://travis-ci.org/yoshuawuyts/wayfarer
146[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/wayfarer/master.svg?style=flat-square
147[7]: https://codecov.io/github/yoshuawuyts/wayfarer
148[8]: http://img.shields.io/npm/dm/wayfarer.svg?style=flat-square
149[9]: https://npmjs.org/package/wayfarer
150[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
151[11]: https://github.com/feross/standard
152[12]: http://github.com/raynos/mercury
153[13]: http://github.com/raynos/virtual-dom