koa-tree-router icon indicating copy to clipboard operation
koa-tree-router copied to clipboard

wildcard issue in group router

Open noahehall opened this issue 3 years ago • 5 comments

unable to specify catchall route handler if a named route exist at the same level

issue:

  • it appears i cannot have a catchall route at the same level of a named route?

purpose:

  • I want to catch all invalid requests at URI good/good/badname

problem:

  • if I already have good/good/goodname
  • koa-tree router throws error `error in v1 handler Error:
  • **wildcard route 'notfound' conflicts with existing children in path '/v1/demo/notfound'`'
    //works
   v1RouterGroup.get('/', v1Handler);

   //works
    v1RouterGroup.get('/demo/pkgcheck', demo.pkgCheckHandler.getPkg);

   // does not work
    v1RouterGroup.get('/demo/*notfound', demo.pkgCheckHandler.notFound);

   //works
    v1RouterGroup.get('/demo/someothershit/*notfound', demo.pkgCheckHandler.notFound);

here is a link to the actual code

here is a link to the handler definition

noahehall avatar May 26 '21 23:05 noahehall

link to line throwing code: https://github.com/steambap/koa-tree-router/blob/b6e8fa9d137734df5e229cf2f174e96c29585334/tree.js#L276

noahehall avatar May 27 '21 02:05 noahehall

I'll try to fix it during the weekend.

I think the original intension of the Golang implementation is that, there should be only one route for any path. With that guarantee, it is possible to use goroutine to do an optimization on the route search. Anyway, this is not relevant in Node.js and should be fixed.

steambap avatar May 27 '21 03:05 steambap

@steambap dope for responding! i was just hacking my way through the source code ;)

if you have an idea of what your going to do fix it, send it my way and hopefully we can share the burden.

the information you've provided is helpful and clarifies why the error is there.

noahehall avatar May 27 '21 03:05 noahehall

Hi @noahehall , if you know how Golang works, you can just copy everything, including the tests, from here: https://github.com/julienschmidt/httprouter/pull/329

steambap avatar May 27 '21 05:05 steambap

I know golang about as good as I know swahili.

I reviewed julienschmidt/httprouter#329 and it doesnt quite solve this issue, however would still be a net benefit.

the issue fixed on httprouter is specific to routes with variable path segments, here we're concerned with catchalls

[update] reviewing the patch that fixes the variable path segment issue provides a clear pathway for fixing this issue.

i’ll tackle it and submit a PR.

noahehall avatar May 27 '21 05:05 noahehall