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

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

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

问题

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

main.go

  1. func main() {
  2. r := mux.NewRouter()
  3. http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
  4. r.HandleFunc("/index", index)
  5. http.ListenAndServe(":8080", r)
  6. }
  7. func index(w http.ResponseWriter, r *http.Request) {
  8. http.ServeFile(w, r, "./static/html/test.html")
  9. }

结构

test.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="/static/css/test.css" />
  5. </head>
  6. <body class="sb-nav-fixed">
  7. asdfasd
  8. </body>
  9. </html>

test.css

  1. body{
  2. height: 100%;
  3. width: 100%;
  4. background-color: brown;
  5. }
英文:

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

  1. func main() {
  2. r := mux.NewRouter()
  3. http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, http.FileServer(http.Dir(&quot;./static/&quot;))))
  4. r.HandleFunc(&quot;/index&quot;, index)
  5. http.ListenAndServe(&quot;:8080&quot;, r)
  6. }
  7. func index(w http.ResponseWriter, r *http.Request) {
  8. http.ServeFile(w, r, &quot;./static/html/test.html&quot;)
  9. }

structure

test.html

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/static/css/test.css&quot; /&gt;
  5. &lt;/head&gt;
  6. &lt;body class=&quot;sb-nav-fixed&quot;&gt;
  7. asdfasd
  8. &lt;/body&gt;
  9. &lt;/html&gt;

test.css

  1. body{
  2. height: 100%;
  3. width: 100%;
  4. background-color: brown;
  5. }

答案1

得分: 1

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

你可以像这样做:

  1. func main() {
  2. http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
  3. http.HandleFunc("/index", index)
  4. http.ListenAndServe(":8080", nil)
  5. }
  6. func index(w http.ResponseWriter, r *http.Request) {
  7. http.ServeFile(w, r, "./static/html/test.html")
  8. }

或者像这样:

  1. func main() {
  2. r := mux.NewRouter()
  3. r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
  4. r.HandleFunc("/index", index)
  5. http.ListenAndServe(":8080", r)
  6. }
  7. func index(w http.ResponseWriter, r *http.Request) {
  8. http.ServeFile(w, r, "./static/html/test.html")
  9. }
英文:

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

you can do it like this

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

or like this

  1. func main() {
  2. r := mux.NewRouter()
  3. r.PathPrefix(&quot;/static/&quot;).Handler(http.StripPrefix(&quot;/static/&quot;, http.FileServer(http.Dir(&quot;./static/&quot;))))
  4. r.HandleFunc(&quot;/index&quot;, index)
  5. http.ListenAndServe(&quot;:8080&quot;, r)
  6. }
  7. func index(w http.ResponseWriter, r *http.Request) {
  8. http.ServeFile(w, r, &quot;./static/html/test.html&quot;)
  9. }

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:

确定