UNPKG

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