【问题标题】:MIME type (“text/plain”) mismatch (X-Content-Type-Options: nosniff)MIME 类型(“text/plain”)不匹配(X-Content-Type-Options: nosniff)
【发布时间】:2019-09-13 21:20:38
【问题描述】:

我使用的是 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))

【问题讨论】:

  • 请提供一个简短的、独立的示例。比如你不指定ngxMappingHandler的实现。
  • 我认为没有必要,因为ngxMappingHandler 在我添加 gorilla/mux 后工作正常,但 css 和 javascript 文件无法加载。
  • 我在使用 react js 和 ajax-solr 时遇到了同样的问题

标签: go mime-types gorilla mux


【解决方案1】:

更改了这一行:

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

进入这个:

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

【讨论】:

  • 为什么会这样?
猜你喜欢
  • 1970-01-01
  • 2020-11-13
  • 1970-01-01
  • 2021-01-10
  • 2020-08-10
  • 2019-11-04
  • 2018-01-13
  • 2020-03-07
  • 2020-03-18
相关资源
最近更新 更多