为什么我无法使用gorilla/mux.Router和net/http.Handle连接CSS文件?

huangapple go评论79阅读模式
英文:

why i can't connect css file using gorilla/mux.Router net/http.Handle

问题

我查看了所有类似的问题,并按照其中的说明连接了文件,但尽管如此,文件仍然无法工作。
我不知道该怎么办,我做错了什么。

main.go

func main() {
    r := mux.NewRouter()
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
    r.HandleFunc("/index", index)
    http.ListenAndServe(":8080", r)
}
func index(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "./static/html/test.html")
}

结构

test.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" type="text/css" href="/static/css/test.css" />
    </head>
    <body class="sb-nav-fixed">
        asdfasd
    </body>
</html>

test.css

body{
    height: 100%;
    width: 100%;
    background-color: brown;
}
英文:

I looked at all similar questions and connected the file as it was said there, but despite this, the file does not work.
I don't know what to do, what did I do wrong

main.go

func main() {
	r := mux.NewRouter()
	http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, http.FileServer(http.Dir(&quot;./static/&quot;))))
	r.HandleFunc(&quot;/index&quot;, index)
	http.ListenAndServe(&quot;:8080&quot;, r)
}
func index(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, &quot;./static/html/test.html&quot;)
}

structure

test.html

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
        &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/static/css/test.css&quot; /&gt;
    &lt;/head&gt;
    &lt;body class=&quot;sb-nav-fixed&quot;&gt;
        asdfasd
    &lt;/body&gt;
&lt;/html&gt;

test.css

body{
    height: 100%;
    width: 100%;
    background-color: brown;
}

答案1

得分: 1

你不能混合使用net/http.Handlegorilla/mux.Router

你可以像这样做:

func main() {
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
	http.HandleFunc("/index", index)
	http.ListenAndServe(":8080", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "./static/html/test.html")
}

或者像这样:

func main() {
	r := mux.NewRouter()
	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
	r.HandleFunc("/index", index)
	http.ListenAndServe(":8080", r)
}
func index(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "./static/html/test.html")
}
英文:

you can't mix net/http.Handle and gorilla/mux.Router

you can do it like this

func main() {
	http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, http.FileServer(http.Dir(&quot;./static/&quot;))))
	http.HandleFunc(&quot;/index&quot;, index)
	http.ListenAndServe(&quot;:8080&quot;, nil)
}
func index(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, &quot;./static/html/test.html&quot;)
}

or like this

func main() {
	r := mux.NewRouter()
	r.PathPrefix(&quot;/static/&quot;).Handler(http.StripPrefix(&quot;/static/&quot;, http.FileServer(http.Dir(&quot;./static/&quot;))))
	r.HandleFunc(&quot;/index&quot;, index)
	http.ListenAndServe(&quot;:8080&quot;, r)
}
func index(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, &quot;./static/html/test.html&quot;)
}

huangapple
  • 本文由 发表于 2022年6月10日 00:34:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/72563793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定