Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 在使用嵌入式文件系统时,如何确保内容类型正确?_Go_Filesystems_Content Type_Static Site - Fatal编程技术网

Go 在使用嵌入式文件系统时,如何确保内容类型正确?

Go 在使用嵌入式文件系统时,如何确保内容类型正确?,go,filesystems,content-type,static-site,Go,Filesystems,Content Type,Static Site,我想在我的二进制文件中创建我的静态站点: //go:embed dist var embededFiles embed.FS func main() { r := mux.NewRouter() // embedded site fsys, err := fs.Sub(embededFiles, "dist") if err != nil { panic(err) } r.Handle("/

我想在我的二进制文件中创建我的静态站点:

//go:embed dist
var embededFiles embed.FS

func main() {
    r := mux.NewRouter()
    
    // embedded site
    fsys, err := fs.Sub(embededFiles, "dist")
    if err != nil {
        panic(err)
    }
    r.Handle("/", http.FileServer(http.FS(fsys)))
(...)

}
调用站点时,我在Chrome中遇到错误,因为内容类型设置不正确:

Refused to apply style from 'http://localhost:1492/css/app.606b59e3.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Refused to apply style from 'http://localhost:1492/css/chunk-vendors.b2711bc7.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
似乎有一种方法可以根据文件的内容进行修改,但我不知道是否以及如何将其用于嵌入式文件系统

编辑:提供的文件以开头

.title{width:30vw}.q-editor.q-editor--default{border:3px solid rgba(0,0,0,.19);border-radius:4px;background-color:#dff}

因此它们应该看起来像CSS文件,并且
fmt.Println(mime.TypeByExtension(“.CSS”)
返回
text/CSS;charset=utf-8

此问题与嵌入式文件系统无关。随机猜测:系统mime.types文件将.css扩展名的类型指定为text/plain。使用此语句调试:
fmt.Println(mime.TypeByExtension(“.css”)
。如果它输出text/plain,请修复mime.types文件或使用
mime.AddExtensionType(.css:“text/css;charset=utf-8”)
Penelope几乎正确:请(它只是简单的;-))。基本上,它首先尝试使用显式设置的
内容类型
,然后使用stock根据文件名的扩展名猜测类型。您还可以看看
mime
包是如何执行这种猜测的。@PenelopeStevens,因为您已经直接跳到修改
mime.types
文件(而且有些系统甚至没有)。但我觉得你有点反应过度:我没有指责你公然撒谎或回答不正确,当然也没有这样做的意图。我不是以英语为母语的人,所以可能是我的用词不太理想。如果是这样,我很抱歉。优先级顺序是(1)应用程序提供的内容类型头,(2)扩展的mime类型,(3)嗅探。我希望内容类型设置为
text/css;charset=utf-8
,因为问题中显示的扩展名是.css。嗅探器确实为问题中的CSS文件返回text/plain。奇怪的我放弃。文件服务器的未找到响应是纯文本。直接在浏览器地址栏中输入CSS文件URL,以确认服务器正在返回该文件。