dream12001 2018-07-03 19:58
浏览 236
已采纳

大猩猩/ mux中的PathPrefix()和Handle(pathString,…)之间有什么区别?

I noticed that there are two ways to specify a path in the gorilla/mux router:

r.PathPrefix("/api").Handler(APIHandler)

And:

r.Handle("/api", APIHandler)

What is the difference?

Also, I don't understand the difference between a router and a route in the context of gorilla/mux.

PathPrefix() returns a route, which has a Handler() method. However, we cannot call Handler() on a router, we must call Handle().

Look at the following example:

r.PathPrefix("/").Handler(http.FileServer(http.Dir(dir+"/public")))

I am trying to serve static files from a public directory. The above expression works without any problem. My HTML and JavaScript are served as expected. However, once I add something to the path, e.g.

r.PathPrefix("/home").Handler(http.FileServer(http.Dir(dir+"/public")))

Then I am getting 404, not found error on localhost:<port>/home.

  • 写回答

1条回答 默认 最新

  • duanmei1894 2018-07-04 00:23
    关注

    Router vs. Route

    Router is a container within which you can register multiple Route instances. The Route interface is largely replicated on Router to enable easy creation of Route instances in the Router.

    Note that all of the Router methods that have the same name as a Route method are wrappers around Router.NewRoute(), which returns a new Route registered to the Router instance.

    For comparison, when you call such methods on an existing Route instance, it returns the same instance for chaining method calls.

    PathPrefix() vs. Path()

    When you specify a path using PathPrefix() it has an implicit wildcard at the end.

    A quote from the overview section of the documentation:

    Note that the path provided to PathPrefix() represents a "wildcard": calling PathPrefix("/static/").Handler(...) means that the handler will be passed any request that matches "/static/*".

    On the other hand, when you specify a path using Path(), there's no such implied wildcard suffix.

    Router.Handle() vs. Router.Path().Handler()

    Router.Handle() is a shortcut for, and therefore equivalent to, executing Router.Path() followed by a call to Route.Handler() on the returned Route.

    Note that this is not the same as calling Router.PrefixPath() followed by a call to Route.Handler, since Router.Handle() does not provide the wildcard suffix.

    Serving files from a path with a prefix

    For your last example, try changing:

    r.PathPrefix("/home").Handler(http.FileServer(http.Dir(dir+"/public")))
    

    To:

    r.PathPrefix("/home/").Handler(http.StripPrefix("/home/", http.FileServer(http.Dir(dir+"/public"))))
    

    Otherwise, it's trying to serve files from dir + "/public" + "/home/"

    An example of this is in the documentation, halfway through the overview.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分