希望在"/"下注册 FileServer,同时也想在浏览器访问 /时发送 index.html 文件,请问有什么办法可以实现呢。google 一下只能找到 15 年 github 上的一个 issue,最后也还是没有解决
1
boboliu Oct 26, 2017 via Android 可以写个 handler,当 URL 为 /时返回 index,别的返回 FileServe
其实最简单的还是把 index 放在目标目录吧(手动滑稽 |
3
freestyle Oct 29, 2017 @zgoing
@boboliu http.FileServer 会自动处理 index.html 的, 你看代码 https://github.com/golang/go/blob/master/src/net/http/fs.go#L592-L605 比如 http.Handle("/", http.FileServer( http.Dir("assets"))), 在 assets 文件夹下如果有 index.html, 就会显示出来 package main import ( "net/http" "log" ) func main() { http.Handle("/", http.FileServer( http.Dir("assets"))) log.Print("http file server start at :8000") log.Fatal( http.ListenAndServe(":8000", nil)) } |