wayfarer
wayfarer copied to clipboard
Route for / catches all requests
> const r = wayfarer()
undefined
> r.on('/', () => console.log('route hit'))
{ [Function: emit]
_trie: Trie { trie: { nodes: [Object] } },
emit: [Circular],
on: [Function: on],
_wayfarer: true }
> r('/')
route hit
undefined
> r('/foobar')
route hit
Expected that second one to not trigger the route.
This looks like it's because of line 19 in trie.js
// strip leading '/' and split routes
const routes = route.replace(/^\//, '').split('/')
Perhaps we should keep the leading slash in? Or is this by design and am I doing something that I shouldn't be doing?
It feels odd though that no error is given nor messaging (maybe this is a documentation issue?)
Naively removing the .replace(/^\//, '') clause causes a ton of failures :)
@toddself ahh, right yeah this should be because the default path is set by default to '' which is then mapped to '/' - think we might need to change the default path to be false or something
To be more specific for people who experience this problem: You can bypass that behavior by setting a custom default path.
e.g.
const wayfarer = require('wayfarer')
const r = wayfarer('/404') // set the custom default path in here
r.on('/', () => console.log('route hit'))
r.emit('/') // logs 'route hit'
r.emit('/foobar') // will throw