go - MIME 类型 (“text/plain” )不匹配(X-Content-Type-Options : nosniff)

标签 go mime-types gorilla mux

我使用 golang net/http 函数并且没有错误,但我需要自定义 URL,因此我实现了 gorilla/mux 路由器,现在收到如下错误:

The resource from “http://localhost:8080/styles.css” was blocked due to MIME type (“text/plain”) mismatch (X-Content-Type-Options: nosniff).
The resource from “http://localhost:8080/main.js” was blocked due to MIME type (“text/plain”) mismatch (X-Content-Type-Options: nosniff).
The resource from “http://localhost:8080/base.js” was blocked due to MIME type (“text/plain”) mismatch (X-Content-Type-Options: nosniff).

之前的代码:

http.Handle("/transcode", http.HandlerFunc(transcodeHandler))
http.Handle("/tctype", http.HandlerFunc(tctypeHandler))
http.Handle("/sse/dashboard", lp.B)
http.Handle("/upload", http.HandlerFunc(uploadHandler))
http.Handle("/", http.FileServer(http.Dir("views")))
fmt.Println("Listening on port: 8080...")
log.Fatalf("Exited: %s", http.ListenAndServe(":8080", nil))

之后的代码:

r := mux.NewRouter()

r.Handle("/ngx/mapping/{name}", http.HandlerFunc(ngxMappingHandler))
r.Handle("/transcode", http.HandlerFunc(transcodeHandler))
r.Handle("/tctype", http.HandlerFunc(tctypeHandler))
r.Handle("/sse/dashboard", lp.B)
r.Handle("/upload", http.HandlerFunc(uploadHandler))
r.Handle("/", http.FileServer(http.Dir("views")))
fmt.Println("Listening on port: 8080...")
log.Fatalf("Exited: %s", http.ListenAndServe(":8080", r))

最佳答案

更改了这一行:

http.Handle("/", http.FileServer(http.Dir("views")))

进入此:

r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("views"))))

关于go - MIME 类型 (“text/plain” )不匹配(X-Content-Type-Options : nosniff),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55832398/

相关文章:

使用 select 的 Golang channel 不会停止

linux - 在 Go 中执行 Shell/Bash

internet-explorer - 如何防止Internet Explorer播放WAV文件?

excel - 如何将XML格式的Excel电子表格传递到IE10 + Office 2010?

go - 在 Golang 中处理 URL 中的动态参数

json - 如何制作包含 [] 数组的 [] 数组和映射

pointers - Golang、指针、函数

python - 使用Python将文件上传到S3(保留原始文件夹结构)并具有正确的MIME类型

使用 gorilla session 时未保存 golang 中的 session 变量

session - Echo session 中间件不保存