如何将CSS下载到NotFoundHandler?

时间:2019-05-27 08:34:54

标签: go gorilla

我无法将CSS上传到NotFoundHandler。 我使用github.com/gorilla/mux。 我该怎么办?

Struct project

我失败的尝试:

package main

import (
    "fmt"
    "net/http"

    "github.com/gorilla/mux"
)

func hello(res http.ResponseWriter, req *http.Request) {
    fmt.Fprint(res, "Hello!")
}

func notFound(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "static/errPage.html")
}

func main() {
    router := mux.NewRouter()
    router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
    router.NotFoundHandler = http.HandlerFunc(notFound)
    router.HandleFunc("/", hello)
    http.ListenAndServe(":8080", router)

}

1 个答案:

答案 0 :(得分:1)

感谢大家,您有空=) 在文档中找到:

router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))