我正在构建我的第一个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"))))
router.ServeFiles("/static/*filepath",http.Dir("static"))