我无法将CSS上传到NotFoundHandler。 我使用github.com/gorilla/mux。 我该怎么办?
我失败的尝试:
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)
}
答案 0 :(得分:1)
感谢大家,您有空=) 在文档中找到:
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))