当我要提供静态HTML时,资产(CSS,JavaScript)的MIME类型也为text/html
,这会导致错误。
./assets/
内部是index.html
,CSS和JavaScript文件。
const rootHtml = "./assets/index.html"
func main() {
http.HandleFunc("/", root)
log.Fatal(http.ListenAndServe(":3000", nil))
}
func root(w http.ResponseWriter, r *http.Request) {
if false {
http.Redirect(w, r, "/other-route/, http.StatusTemporaryRedirect)
} else {
http.ServeFile(w, r, rootHtml) // This should handle the index.html and all other resources
}
}
资产目录:
assets/
|-- js/
|-- css/
|-- index.html