1. 首页
  2. 主题
  3. Go Web开发

golang静态资源服务器(http2,https),前端无法正常解析js

q1045243113 · · 2091 次点击
### 问题: 想尝试一下golang的http2功能,但是遇到一个问题, js文件无法正常解析... #### go version go1.10.2 windows/amd64 详情: 后端代码 ``` go func main() { var server http.Server http2.VerboseLogs = true server.Addr = ":8080" http2.ConfigureServer(&server, &http2.Server{}) // 先把css和脚本服务上去 http.Handle("/static/",http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) http.Handle("/views/",http.StripPrefix("/views/", http.FileServer(http.Dir("static/views/")))) http.HandleFunc("/sayHello", sayHello) logger.Info("正在启动服务器...") // 启用http2 err := server.ListenAndServeTLS("./keystore/cert.crt", "./keystore/rsa_private.key") //前端不能正常解析js文件 //err := server.ListenAndServe() // 前端可以正常解析js文件 if err != nil { logger.Errorf("服务器启动错误:%v", err) } } func sayHello(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) } ``` 前端代码: ``` html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> Hello world! </div> <script src="/static/js/vue.js" type="text/javascript"></script> <script> </script> </body> </html> ``` 项目结构: ![QQ截图20180605130004.png](https://static.studygolang.com/180605/257f6aeaae38487805b4acdcd4610c27.png) **** 不能正常解析的情况 **** ![QQ截图20180605130256.png](https://static.studygolang.com/180605/7a5d34c961ebd449e7e1bc83cc5997fc.png) 前端错误信息: Refused to execute script from 'https://localhost:8080/static/js/vue.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled. 注: err := server.ListenAndServeTLS("./keystore/cert.crt", "./keystore/rsa_private.key") //用这一句,前端不能正常解析js文件 err := server.ListenAndServe() // 用这一句, 前端可以正常解析js文件
https://github.com/golang/go/issues/24513 `The proposal is to automatically include the X-Content-Type-Options: nosniff header when Content-Type is one of the values known to trigger content sniffing: text/plain, application/octet-stream, application/unknown, unknown/unknown, */* or an invalid value without a slash.` 这个修改要到go1.11才发布,但x/net/http2已经改了,所以现在用go1.10的http会在content-type为text/plain时检查文件内容看实际是否可执行
#14
更多评论
各位大佬请帮忙指点一下, 十分感激
#1
怎么都有没有人回复,是我的问题没有问清楚么
#2

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中