wayfarer icon indicating copy to clipboard operation
wayfarer copied to clipboard

Route for / catches all requests

Open toddself opened this issue 9 years ago • 3 comments

> 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.

toddself avatar Jul 26 '16 22:07 toddself

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 avatar Jul 26 '16 23:07 toddself

@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

yoshuawuyts avatar Jul 27 '16 10:07 yoshuawuyts

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

marcbachmann avatar Dec 29 '17 21:12 marcbachmann