我正在构建我的第一个 Go web 项目,当我加载我的页面时,我在浏览器控制台上收到此错误
Refused to apply style from 'http://localhost:8080/static/css/processor-auth.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
我不确定我做错了什么,因为我已经添加了这段代码来加载静态文件
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))
这是我的 main.go 文件的样子:
package main
import(
"net/http"
"os"
"html/template"
"github.com/julienschmidt/httprouter"
)
// Auth struct handler
type auth struct{}
func (auth *auth) ServeHTTP(w http.ResponseWriter, r *http.Request){
wd,_:= os.Getwd()
t := template.Must(template.ParseFiles(wd+"/templates/processor/auth.html"))
err:=t.Execute(w,nil)
if err !=nil{
http.Error(w,"Could not execute template",500)
}
}
func main(){
router:= httprouter.New()
// set the static files
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))
router.Handler("GET","/auth",&auth{})
server := http.Server{
Addr:"127.0.0.1:8080",
Handler:router,
}
server.ListenAndServe()
}
编辑:解决了问题
因为我用作httprouter
多路复用器,所以我无法使用
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))
我必须更新到 httprouter 的 ServeFiles 函数并将代码更新为 router.ServeFiles("/static/*filepath",http.Dir("static"))
拉丁的传说
梵蒂冈之花
素胚勾勒不出你
相关分类