dongxu4023
2017-10-30 07:04
浏览 104
已采纳

使用Gorilla Mux提供静态HTML

I'm using gorilla serve mux to serve static html files.

r := mux.NewRouter()
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public"))).Methods("GET")

I do have a Index.html file inside the public folder as well as other html files.

When browsing the site I get all the content of the folder instead of the default Index.html.

I came from C# and I know that IIS takes Index.html as default but it is possible to select any page as a default.

I wanted to know if there's a proper way to select a default page to serve in Gorilla mux without creating a custom handler/wrapper.

图片转代码服务由CSDN问答提供 功能建议

我正在使用大猩猩服务多路复用器来提供静态html文件。

   r:= mux.NewRouter()
r.PathPrefix(“ /”)。Handler(http.FileServer(http.Dir(“ ./ public”)))。Methods(“ GET”)
   
 
 

我确实在公用文件夹以及其他html文件中都有一个 Index.html 文件。

浏览该网站时,我得到的是文件夹的所有内容,而不是默认的Index.html。

我来自C#,我知道IIS使用 Index.html 作为默认设置,但可以选择任何页面作为默认页面。

我想知道是否存在一种适当的方法来选择默认页面以在Gorilla Mux中提供服务而无需创建自定义处理程序 /wrapper。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • duanluangua8850 2017-10-30 13:56
    最佳回答

    After grabthefish mentioned it i decided to check the actual code of gorilla serve mux. This code is taken from net/http package which Gorilla mux is based on.

    func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, 
    redirect bool) {
    const indexPage = "/index.html"
    
    // redirect .../index.html to .../
    // can't use Redirect() because that would make the path absolute,
    // which would be a problem running under StripPrefix
    if strings.HasSuffix(r.URL.Path, indexPage) {
        localRedirect(w, r, "./")
        return
    }
    

    the code request the index file to be index.html in lower case so renaming my index file solved it. thank you grabthefish!

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题