go - 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))
解决方案
改变了这一行:
http.Handle("/", http.FileServer(http.Dir("views")))
进入这个:
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("views"))))
推荐阅读
- asp.net-mvc - 如何使 TempData 文本的某些部分变为粗体?
- c - 为什么会这样?ANSI 在数学函数中使用时是否会将以十六进制“形式”写入的值转换为十进制相等?
- php - 没有斜杠的 nginx 配置
- c++ - 没有花括号的枚举形式
- python - 将 np.array 添加到列表后尺寸丢失
- azure - Azure powershell - 如何拉
- javascript - 如何在javascript中的dd-mm-yyyy中显示dob?
- matlab - Border of a scatter plot in Matlab: constructing it and colouring it
- apache-spark - Spark JavaRDD 与 JavaPairRDD?
- tweepy - 如何使用 tweepy 获取推文的全文?