Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route for / catches all requests #44

Open
toddself opened this issue Jul 26, 2016 · 3 comments
Open

Route for / catches all requests #44

toddself opened this issue Jul 26, 2016 · 3 comments
Labels

Comments

@toddself
Copy link

> 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
Copy link
Author

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 :)

@yoshuawuyts
Copy link
Member

@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

@marcbachmann
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants