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

Cannot find route to use #101

Closed
mlovrovich opened this issue Nov 7, 2018 · 9 comments
Closed

Cannot find route to use #101

mlovrovich opened this issue Nov 7, 2018 · 9 comments

Comments

@mlovrovich
Copy link

    const http = require('http')
    const router = require('find-my-way')()

    router.on('GET', '/:namespace/:type/:id', (req, res, params) => {
        res.end('{"message":"hello world"}')
    })

    router.on('POST', '/:namespace/jobs/:name/run', (req, res, params) => {
        res.end('{"message":"hello world"}')
    })

    const server = http.createServer((req, res) => {
        router.lookup(req, res)
    })

    server.listen(4000, err => {
        if (err) throw err
        console.log('Server listening on: http://localhost:4000')
    })

Works as expected

GET http://localhost:4000/test_namespace/test_type/test_id
GET http://localhost:4000/test_namespace/jobss/test_id

Does not work

GET http://localhost:4000/test_namespace/jobs/test_id

@delvedor
Copy link
Owner

Hello!
Can you retry with the latest release?

@mlovrovich
Copy link
Author

@delvedor hello, the same issue is happening with the latest release 1.17.1

@mcollina
Copy link
Collaborator

I think the only way to fix this us to have a different trie for every http method.
It’s a big refactoring, and I fear neither Tomas or myself have time now.

@IpaliboWhyte
Copy link
Contributor

IpaliboWhyte commented Feb 7, 2019

Another way to fix this issue could be storing some useful information on each trie node and use those information to make decision in Node.prototype.findChild and Node.prototype.findVersionChild

@mcollina
Copy link
Collaborator

mcollina commented Feb 7, 2019

@IpaliboWhyte what would you have in mind?

@IpaliboWhyte
Copy link
Contributor

IpaliboWhyte commented Feb 8, 2019

@mcollina Hey, sorry for the late reply. The problem I see so far is that we go too far in the tree. In this case we go too far to search for test_namespace/jobs/test_id in the node for route: /test_namespace/jobs/:name/run.

This issue is also reproducible with these two routes
/jobs/:name/run
/:type/:id

and this search query => jobs/test_id

In theory, if nodes have some stored property which describes the subpath length of a route (based on slash delimiters), by using the search query subpath length we could do a best fit scan to return a more accurate child node. My guess is that findChild will calls this first before proceeding.

Example:

=> /:type/:id (length of 2)
=> /jobs/:name/run (length of 3)

The path /jobs/test_id would have a length of 2, in that case we return the child node /:type/:id. It will be useless to go to /jobs/:name/run as it has a length of 3.

Just my thought for now, hopefully this makes sense :)

@delvedor
Copy link
Owner

delvedor commented Feb 8, 2019

Not sure that the approach you describe will work.
We are using a radix tree, which means that a Node can contain multiple slashes.
Take the following example:

Route 1: /hello/work
Route 2: /hello/wonderful/home/kitchen
Route 3: /heaven/cloud

The generated tree will be

/he
  |-aven/cloud
  |-llo/wo
     |-rk
     |-nderful/home/kitchen

If we start splitting on the slash when a new request comes in we will lose all the benefits of the above data structure, and that point it will be a better idea refactor the entire module (already did it, the performances were not as good).

@IpaliboWhyte
Copy link
Contributor

good point, might affect performance alot. I'll try to take a deep dive into this when I get some time :)

ivan-tymoshenko added a commit to ivan-tymoshenko/find-my-way that referenced this issue Apr 25, 2022
ivan-tymoshenko added a commit to ivan-tymoshenko/find-my-way that referenced this issue Apr 25, 2022
@ivan-tymoshenko
Copy link
Collaborator

This issue was fixed. I added a test here #261. We can close this.

mcollina pushed a commit that referenced this issue Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants