英文:
Serving static content and handling 404 not found with Gorilla toolkit
问题
最近我问过关于使用Gorilla mux来提供静态内容和处理404错误的问题;当使用Handle而不是PathPrefix时,应用程序可以提供根页面(http://localhost:8888):
func main() {
r := mux.NewRouter()
r.HandleFunc("/myService", ServiceHandler)
r.Handle("/", http.FileServer(http.Dir("./static")))
r.NotFoundHandler = http.HandlerFunc(notFound)
l, _ := net.Listen("tcp", "8888")
http.Serve(l, r)
}
然而,对于根页面内的页面请求(例如http://localhost:8888/demo/page1.html),会被404处理程序拦截。有没有办法在捕获不存在的页面或服务请求的同时防止这种情况发生?这是目录结构:
...
main.go
static\
| index.html
demo\
page1.html
demo.js
demo.css
| jquery\
| <js files>
| images\
| <png files>
之前的问题:
我正在使用Gorilla mux工具包来处理Web服务器应用程序中的HTTP请求:
func main() {
r := mux.NewRouter()
r.HandleFunc("/myService", ServiceHandler)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static")))
l, _ := net.Listen("tcp", "8888")
http.Serve(l, r)
}
我想为无效的URL添加一个处理程序,但它从未被调用:
func main() {
r := mux.NewRouter()
r.HandleFunc("/myService", ServiceHandler)
r.NotFoundHandler = http.HandlerFunc(notFound)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
l, _ := net.Listen("tcp", "8888")
http.Serve(l, r)
}
如果我移除静态处理程序,未找到处理程序会被调用。然而,应用程序需要从非绝对路径提供静态内容。有没有办法将其与404处理结合起来?
英文:
I recently asked about serving static content and handling 404 with Gorilla mux; when using Handle instead of PathPrefix, the app can serve the root page (http://localhost:8888):
func main() {
r := mux.NewRouter()
r.HandleFunc("/myService", ServiceHandler)
r.Handle("/", http.FileServer(http.Dir("./static")))
r.NotFoundHandler = http.HandlerFunc(notFound)
l, _ := net.Listen("tcp", "8888")
http.Serve(l, r)
}
However requests for pages within the root page (e.g. http://localhost:8888/demo/page1.html) get intercepted by the 404 handler. Is there any way to prevent this, while catching requests for non-existent pages or services? This is the directory structure:
...
main.go
static\
| index.html
demo\
page1.html
demo.js
demo.css
| jquery\
| <js files>
| images\
| <png files>
Previous Question:
I am using the Gorilla mux toolkit to handle http requests in a web server app:
func main() {
r := mux.NewRouter()
r.HandleFunc("/myService", ServiceHandler)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static")))
l, _ := net.Listen("tcp", "8888")
http.Serve(l, r)
}
I want to add a handler for invalid URLS, but it is never called:
func main() {
r := mux.NewRouter()
r.HandleFunc("/myService", ServiceHandler)
r.NotFoundHandler = http.HandlerFunc(notFound)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
l, _ := net.Listen("tcp", "8888")
http.Serve(l, r)
}
If I remove the static handler, the not found handler is called. However, the app needs to serve the static content, from a non-absolute path. Is there a way to combine that with 404 handling?
答案1
得分: 1
我怀疑r.PathPrefix("/") .Handler()
会匹配任何路径,使得notfound
处理程序无效。
如route.go
中所述:
//请注意,它不会特殊处理斜杠(“/foobar/”将与前缀“/foo”匹配),因此您可能希望在此处使用尾部斜杠。
如果您正在使用PathPrefix
(如这些测试中所示),请将其用于特定路径,而不是通用的“/”。
英文:
I suspect r.PathPrefix("/").Handler()
would match any path, making the notfound
handler useless.
As mentioned in "route.go
":
// Note that it does not treat slashes specially
// ("`/foobar/`" will be matched by the prefix "`/foo`")
// so you may want to use a trailing slash here.
If you are using PathPrefix
(as in those tests), use it for a specific path, not for the generic "/
".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论