第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Golang Gorilla mux 與 http.FileServer 返回 404

Golang Gorilla mux 與 http.FileServer 返回 404

Go
慕桂英546537 2021-07-13 09:10:18
我看到的問(wèn)題是我試圖將http.FileServer與 Gorilla mux Router.Handle 功能一起使用。這不起作用(圖像返回 404)。myRouter := mux.NewRouter()myRouter.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))這有效(圖像顯示正常)..http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))下面簡(jiǎn)單的go web server程序,顯示問(wèn)題...package mainimport (    "fmt"    "net/http"    "io"    "log"    "github.com/gorilla/mux")const (    HomeFolder = "/root/test/")func HomeHandler(w http.ResponseWriter, req *http.Request) {    io.WriteString(w, htmlContents)}func main() {    myRouter := mux.NewRouter()    myRouter.HandleFunc("/", HomeHandler)    //    // The next line, the image route handler results in     // the test.png image returning a 404.    // myRouter.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))    //    myRouter.Host("mydomain.com")    http.Handle("/", myRouter)    // This method of setting the image route handler works fine.    // test.png is shown ok.    http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))    // HTTP - port 80    err := http.ListenAndServe(":80", nil)    if err != nil {        log.Fatal("ListenAndServe: ", err)        fmt.Printf("ListenAndServe:%s\n", err.Error())    }}const htmlContents = `<!DOCTYPE HTML><html>  <head>    <title>Test page</title>    <meta charset = "UTF-8" />  </head>  <body>    <p>        <img src="/images/test.png" height="640" width="480">    </p>  </body></html>
查看完整描述

2 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

截至 2015 年 5 月,gorilla/mux包仍然沒(méi)有版本發(fā)布。但是現(xiàn)在問(wèn)題不同了。不是myRouter.Handle不匹配 url 并且需要正則表達(dá)式,它確實(shí)如此!但http.FileServer需要從 url 中刪除前綴。下面的例子工作正常。


ui := http.FileServer(http.Dir("ui"))

myRouter.Handle("/ui/", http.StripPrefix("/ui/", ui))

請(qǐng)注意,上面的示例中沒(méi)有 /ui/ {rest}。您還可以包裝http.FileServer到 logger gorilla/handler 中,并看到請(qǐng)求到 FileServer 和響應(yīng) 404 出去。


ui := handlers.CombinedLoggingHandler(os.Stderr,http.FileServer(http.Dir("ui"))

myRouter.Handle("/ui/", ui) // getting 404

// works with strip: myRouter.Handle("/ui/", http.StripPrefix("/ui/", ui))


查看完整回答
反對(duì) 回復(fù) 2021-07-19
  • 2 回答
  • 0 關(guān)注
  • 358 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)