繁体   English   中英

在http.HandlerFunc中调用http.FileServer

[英]Calling http.FileServer in an http.HandlerFunc

所以最近我遇到了一些麻烦...这是我的代码:

https://gist.github.com/anonymous/af1e6d922ce22597099521a4b2cfa16f

我的问题:我正在尝试从以下文件夹提供一些HTML文件: ./docs/html 我的文件夹结构:

.
├── docs
│   └── html
│       ├── index.html
│       └── rest.html
└── main.go

你会注意到在要点我打电话的ServeHTTP的方法http.HandlerFunc ServeDocs,然后通过路由器(去mux.Router )。 由于某些原因,我遇到的问题是在localhost:8080/提供的唯一文件是index.html ,当我导航到localhost:8080/rest.html我得到了404。

真正奇怪的是,当我删除所有路由器代码并执行以下操作时:

fs := http.FileServer(http.Dir("./docs/html"))
http.Handle("/", fs)

log.Println("Listening...")
http.ListenAndServe(":3000", nil)

一切正常。 有人知道发生了什么吗? 我花了几个小时试图解决这个问题,但最终我还是放弃了。

如果您使用mux.RouterPath方法,则可以使用

 r.Methods(route.Method).Name(route.Name).Handler(handler) r.Path(route.Pattern) 

而不是mux.RoutePath方法(下面的删除线)

r.Methods(route.Method) .Path(route.Pattern) .Name(route.Name).Handler(处理程序)

我对gorilla/mux不太熟悉,所以找不到确切的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM