美文网首页
go 标准库 http server 遇到的坑

go 标准库 http server 遇到的坑

作者: 风亡小窝 | 来源:发表于2019-05-18 18:39 被阅读0次

1. ./index.html会被自动重定向到 ./

例:

原始 重定向后
static/index.html static/
/index.html /

而标准库则会把 ./ 当作 ./index.html 处理

2. url 与 dir 的对应关系

http.Handle("/f/", http.FileServer(http.Dir("./static")))

req_url = "/f/f1" 时,

用过其他web框架的

大都会以为是请求文件 ./static/f1

而实际上则是请求文件 ./static/f/f1

要达到想要的效果则要使用 http.StripPrefix,去掉 /f/前缀

http.Handle("/f/", http.StripPrefix("/f/", http.FileServer(http.Dir("./static"))))

另外注意是 /f/ ,而不是 /f

//不会是你想要的效果
http.Handle("/f", http.StripPrefix("/f/", http.FileServer(http.Dir("./static"))))

相关文章

网友评论

      本文标题:go 标准库 http server 遇到的坑

      本文链接:https://www.haomeiwen.com/subject/xewtzqtx.html