我有一個去靜態內容網站問題。我的結構如下所示:|-Go|--static/*|--go.mod|--main.go當我以這種方式提供靜態內容時 router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) { http.FileServer(http.Dir("./")).ServeHTTP(res, req) })我可以在網址中看到go.mod的內容 https://example.com/go.mod然后我把代碼改成了 router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) { http.FileServer(http.Dir("./static/*")).ServeHTTP(res, req) })我也試過 router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) { http.FileServer(http.Dir("./static/")).ServeHTTP(res, req) })我無法再在網址中看到go.mod的內容 https://example.com/go.mod但是我的徽標不再可見<img src="/static/imgs/logo.png" alt="logo" id="logo"/>我怎么能只提供目錄的內容?./static/
1 回答

慕娘9325324
TA貢獻1783條經驗 獲得超4個贊
要在靜態目錄下服務器內容:
http.FileServer(http.Dir("./static/"))
然后,對 /imgs/logo 的請求.png將是來自 ./static/imgs/logo.png 的服務器(不是 /static/img/logo.png)
如果您還想在網址中包含 /static/,請執行以下操作:
http.StripPrefix("/static/",http.FileServer(http.Dir("./static/")))
- 1 回答
- 0 關注
- 93 瀏覽
添加回答
舉報
0/150
提交
取消