### 问题: 想尝试一下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文件
说错了,是现在用go1.10的http server不会加X-Content-Type-Options: nosniff 这个header,导致客户端(浏览器)会检查文件内容看实际是否可执行
#15
更多评论