golang中的http.FileServer通常要跟http.StripPrefix結合使用

    用go寫一個文件服務器很簡單:

    http.handle(“/”,  http.FileServer(http.Dir(“doc”))

   http.ListenAndServe(":8888”, nil)

   打來localhost:8888,就能看到doc目錄下的所有文件。

   但如果,你想用localhost:8888/doc來顯示進入文件目錄,則需要

   http.Handle(“/doc", http.StripPrefix(“/doc", http.FileServer(http.Dir(“doc"))))

   http.StripPrefix用於過濾request,參數裏的handler的request過濾掉特定的前序,只有這樣,才能正確顯示文件目錄。 

    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章