Go语言 无法导入css,因为其MIME类型[重复]

ztyzrc3y  于 5个月前  发布在  Go
关注(0)|答案(1)|浏览(58)

此问题在此处已有答案

Getting MIME type ('text/plain') error in Golang while serving CSS(3个答案)
上个月关门了。
我正在做一个简单的golang网络应用程序。试图添加一些css,但面临下一个问题:
“拒绝应用"http://localhost:8080/api/user/static/styles.css”中的样式,因为其MIME类型(“text/plain”)不是支持的样式表MIME类型,并且启用了严格的MIME检查”
当前项目-结构

project-root
├── cmd
│   └── app
│       └── main.go
└── templates
    └── static
        └── styles.css
    └── register.html

字符串
我尝试了一堆CSS文件导入选项,但没有一个工作:

<link rel="stylesheet" type="text/css" href="templates/static/styles.css"/>
    <link type="text/css" href="static/styles.css">
    <link type="text/css" href="/templates/static/styles.css">
    <link type="text/css" href="./static/styles.css">

    <link rel="stylesheet" href="templates/static/styles.css">
    <link rel="stylesheet" href="templates/static/styles.css">

    <link rel="stylesheet" type="text/css" href="/styles.css"/>
    <link rel="stylesheet" type="text/css" href="./static/styles.css"/>
    <link rel="stylesheet" href="./static/styles.css">


还试图添加

<base href="/">


但对我没用
尝试了这里的解决方案https://stackoverflow.com/questions/48248832/stylesheet-not-loaded-because-of-mime-type,它也没有工作
我使用github.com/julienschmidt/httprouter来导航请求如下:

func NewRouter(userController *controller.UserController, htmlController *controller.HTMLController) *httprouter.Router {
    router := httprouter.New()

    staticHandler := http.StripPrefix("/templates/static/", http.FileServer(http.Dir("templates/static")))
    router.GET("/templates/static/*filepath", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
        staticHandler.ServeHTTP(w, r)
    })

    //router.ServeFiles("/templates/static/*filepath", http.Dir("templates/static"))
    //router.ServeFiles("/static/*filepath", http.Dir("C:\\Users\\vadim\\IdeaProjects\\GoLang\\sushkof_test\\templates\\static"))

    //http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
    //router.ServeFiles("/static/*filepath", http.Dir("templates/static"))

    router.GET("/api/user/register", htmlController.RegisterHandler)
    router.GET("/api/user/home", htmlController.HomeHandler)

    router.POST("/api/user/register_user", userController.Save)
    //router.ServeFiles("/templates/*filepath", http.Dir("templates"))
    return router
}


或许我做错了什么?
很抱歉有这么多的注解代码,但是我不得不展示我已经使用过的代码。我的大脑已经融化了。

**UPD:**如果我把“main.go”目录改为project-root/main.go,那么相对路径就可以了,但是如果“maing-go”在“cmd/app”中,即使我使用

router.ServeFiles("/templates/static/*filepath", http.Dir("templates/static"))


<link type="text/css" href="/templates/static/styles.css">


这可能是IDE(IntelliJ IDEA)问题吗?

62lalag4

62lalag41#

浏览器报告MIME错误,因为浏览器收到的是“未找到”响应,而不是CSS文件。
此语句用于注册文件处理程序:

router.ServeFiles("/templates/static/*filepath", http.Dir("templates/static"))

字符串
使用以下HTML加载CSS文件:

<link type="text/css" href="/templates/static/styles.css">


应用程序读取相对路径为templates/static/styles.css的档案。若要解析该档案的相对路径,请在将working directory设定为项目根目录的情况下执行应用程序。
Run the configured router on the playground!的一个。

相关问题