1 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
更改此行:
// handles '/' and *ONLY* '/'
Router.Handle("/",
http.StripPrefix("/", http.FileServer(http.Dir(publicDir))))
對(duì)此:
// handles '/' and all sub-routes
Router.PathPrefix("/").Handler(
http.StripPrefix("/",http.FileServer(http.Dir(publicPath))))
基本上,在您的原始代碼中,路由器/正在處理此路徑并且僅處理該路徑(無(wú)子路由)。
您可能想知道為什么您的原始代碼至少對(duì)一個(gè)文件“有效”(index.html)。原因是http.FileServer給定的路徑是目錄 - 而不是文件 - 將默認(rèn)為索引頁(yè)面文件提供服務(wù)index.html(請(qǐng)參閱FileServer 源代碼)。
UsingPathPrefix允許 (fileserver) 處理程序接受path 下的所有URL 路徑/。
- 1 回答
- 0 關(guān)注
- 107 瀏覽
添加回答
舉報(bào)