首页 > 解决方案 > MIME 类型(“text/plain”)不匹配(X-Content-Type-Options: nosniff)

问题描述

我正在使用 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))

标签: gomime-typesgorillamux

解决方案


改变了这一行:

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

进入这个:

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

推荐阅读