【问题标题】:Priority among route patterns路由模式之间的优先级
【发布时间】:2013-07-31 08:28:51
【问题描述】:

我正在运行 thin 服务器(没有任何 Web 应用程序框架)。在路由模式中,匹配模式的顺序似乎没有区别。我是否这样做:

Rack::Handler::Thin.run(Rack::Builder.new do
  map("/"){...}
  map("/foo/"){...}
end, Port: 3000)

Rack::Handler::Thin.run(Rack::Builder.new do
  map("/foo/"){...}
  map("/"){...}
end, Port: 3000)

localhost:3000/foo/ 的请求将被map("/foo/"){...} 而不是map("/"){...} 正确接收。这个优先级是如何确定的?

对于某些 Web 应用程序框架,例如在 Sinatra 中,它显示为 Routes are matched in the order they are defined. The first route that matches the request is invoked,而我的应用程序设置不是这种情况。

【问题讨论】:

    标签: ruby url-routing rack thin


    【解决方案1】:

    https://github.com/rack/rack/blob/master/lib/rack/urlmap.rb

    在 Rack::URLMap 中没有确定优先级。它与您提供的资源的完整路径匹配

    map(){ ... }
    

    【讨论】:

    • 您的回答没有回答我的问题,但是您链接的页面有一个描述为URLMap dispatches in such a way that the longest paths are tried first, since they are most specific,它回答了我的问题。并且第38--40行有相关代码sort_by do |(host, location, _, _)| [host ? -host.size : INFINITY, -location.size] end
    猜你喜欢
    • 2014-03-15
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2016-08-04
    相关资源
    最近更新 更多