dongrong9938 2019-02-22 21:35
浏览 246

提供CSS时,在Golang中出现MIME类型(“文本/纯文本”)错误

I am building my first Go web project and I am getting this error on the browser console when I load my page

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.

I am not sure what I am doing wrong because I have already added this code to load the static files

http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))

This is how my main.go file looks like:

 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()
}

Folder Structure

Edit: Solved the issue

Since I was using httprouter as my multiplexer I could not use

http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))

I had to update to the httprouter's ServeFiles function and update the code to router.ServeFiles("/static/*filepath",http.Dir("static"))

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计