亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

僅提供選定的文件夾

僅提供選定的文件夾

Go
白衣染霜花 2022-08-15 19:42:24
我有一個去靜態內容網站問題。我的結構如下所示:|-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/")))


查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號