Golang Gorilla mux,http.FileServer返回404
我看到的问题是,我试图将http.FileServer与Gorilla mux Router.Handle函数一起使用 这不起作用(图像返回404) 这是可行的(图像显示正常) 下面是简单的go web服务器程序,显示问题Golang Gorilla mux,http.FileServer返回404,go,gorilla,Go,Gorilla,我看到的问题是,我试图将http.FileServer与Gorilla mux Router.Handle函数一起使用 这不起作用(图像返回404) 这是可行的(图像显示正常) 下面是简单的go web服务器程序,显示问题 package main import ( "fmt" "net/http" "io" "log" "github.com/gorilla/mux" ) const ( HomeFolder = "/root/test/"
package main
import (
"fmt"
"net/http"
"io"
"log"
"github.com/gorilla/mux"
)
const (
HomeFolder = "/root/test/"
)
func HomeHandler(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, htmlContents)
}
func main() {
myRouter := mux.NewRouter()
myRouter.HandleFunc("/", HomeHandler)
//
// The next line, the image route handler results in
// the test.png image returning a 404.
// myRouter.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))
//
myRouter.Host("mydomain.com")
http.Handle("/", myRouter)
// This method of setting the image route handler works fine.
// test.png is shown ok.
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))
// HTTP - port 80
err := http.ListenAndServe(":80", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
fmt.Printf("ListenAndServe:%s\n", err.Error())
}
}
const htmlContents = `<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Test page</title>
<meta charset = "UTF-8" />
</head>
<body>
<p align="center">
<img src="/images/test.png" height="640" width="480">
</p>
</body>
</html>
`
主程序包
进口(
“fmt”
“net/http”
“io”
“日志”
“github.com/gorilla/mux”
)
常数(
HomeFolder=“/root/test/”
)
func HomeHandler(带http.ResponseWriter,req*http.Request){
io.WriteString(带HTML内容)
}
func main(){
myRouter:=mux.NewRouter()
myRouter.HandleFunc(“/”,HomeHandler)
//
//下一行,图像路由处理程序将生成
//test.png图像返回404。
//myRouter.Handle(“/images/”,http.StripPrefix(“/images/”,http.FileServer(http.Dir(HomeFolder+“images/”)))
//
myRouter.Host(“mydomain.com”)
http.Handle(“/”,myRouter)
//这种设置映像路由处理程序的方法工作正常。
//test.png显示为ok。
http.Handle(“/images/”,http.StripPrefix(“/images/”,http.FileServer(http.Dir(HomeFolder+“images/”)))
//HTTP-端口80
错误:=http.ListenAndServe(“:80”,无)
如果错误!=零{
log.Fatal(“ListendServe:,错误)
fmt.Printf(“ListendServe:%s\n”,err.Error())
}
}
常量htmlContents=`
测试页
`
我把这个贴在了golang nuts讨论组上,得到了
标准net/http ServeMux(这是您在使用http.Handle时使用的标准处理程序)和mux路由器有不同的地址匹配方式
查看和之间的差异
所以基本上,http.Handle('/images/',…)
匹配'/images/whatever',而myRouter.Handle('/images/',…)
只匹配'/images/',如果你想处理'/images/whatever',你必须
选项1-在路由器中使用正则表达式匹配
myRouter.Handle(“/images/{rest}”,
http.StripPrefix(“/images/”,http.FileServer(http.Dir(HomeFolder+“images/”)))
选项2-在路由器上使用PathPrefix方法:
myRouter.PathPrefix(“/images/”)处理程序(http.StripPrefix(“/images/”),
文件服务器(http.Dir(HomeFolder+“images/”))
截至2015年5月,软件包仍然没有版本发布。但现在问题不同了。这并不是说myRouter.Handle
与url不匹配,需要regexp,而是这样!但是http.FileServer
要求从url中删除前缀。下面的例子很好用
ui := http.FileServer(http.Dir("ui"))
myRouter.Handle("/ui/", http.StripPrefix("/ui/", ui))
注意,在我们的示例中没有/ui/{rest}。您还可以将http.FileServer
包装到logger gorilla/handler中,并查看对文件服务器的请求和对404的响应
ui := handlers.CombinedLoggingHandler(os.Stderr,http.FileServer(http.Dir("ui"))
myRouter.Handle("/ui/", ui) // getting 404
// works with strip: myRouter.Handle("/ui/", http.StripPrefix("/ui/", ui))
+1#2在当前项目中对我来说是成功的(在我偶然发现这个答案之前。只是让读者放心,#2是我正在使用和工作的东西)。只需补充一点,请记住,如果你和我一样是golang新手,golang不喜欢in#2格式的语句。在golang停止抱怨需要逗号之前,我需要将所有三行合并成一行(语法没有变化,只有空格的变化)#2是正确的答案,它的格式会给像我这样的新手和新手带来问题。你是对的,我不得不把这句话放在一行。非常感谢你让我摆脱了痛苦,PathPrefix和StripPrefix对我很有效,我从
r.Handle(“/”,fs)开始)
到r.PathPrefix(“/”).Handler(http.StripPrefix(“/”,fs))
@SamGamgee-您可以进一步删除http.StripPrefix()
,以得到r.PathPrefix(“/”).Handler(fs)
router的默认Handle
方法仍然以与以前相同的方式创建route
。我尝试了您上面提到的方法,“/ui/”
不会将所有路径与“/ui/”
前缀匹配@dodgy_编码器的解决方案是正确的。使用默认的处理程序
方法包含正则表达式,或者使用路径前缀
ui := http.FileServer(http.Dir("ui"))
myRouter.Handle("/ui/", http.StripPrefix("/ui/", ui))
ui := handlers.CombinedLoggingHandler(os.Stderr,http.FileServer(http.Dir("ui"))
myRouter.Handle("/ui/", ui) // getting 404
// works with strip: myRouter.Handle("/ui/", http.StripPrefix("/ui/", ui))