dpxua26604 2017-01-21 00:44
浏览 80
已采纳

在http.HandlerFunc中调用http.FileServer

so I've had some trouble with this lately... Here is my code:

https://gist.github.com/anonymous/af1e6d922ce22597099521a4b2cfa16f

My problem: I'm trying to serve up some HTML files from a folder: ./docs/html. My folder structure:

.
├── docs
│   └── html
│       ├── index.html
│       └── rest.html
└── main.go

You'll notice in the gist I am calling the ServeHTTP method on the http.HandlerFunc ServeDocs, which is then going through a router (mux.Router). The problem I'm having is for some reason the only file being served up at localhost:8080/ is index.html, and when I navigate to localhost:8080/rest.html I get a 404.

The really odd part is that when I remove all the router code and do something like the following:

fs := http.FileServer(http.Dir("./docs/html"))
http.Handle("/", fs)

log.Println("Listening...")
http.ListenAndServe(":3000", nil)

Everything works as it should. Anybody know what's going on? I've spent hours trying to figure this out and I've finally given up.

  • 写回答

1条回答 默认 最新

  • dou2347 2017-01-21 02:48
    关注

    It works if you use mux.Router's Path method

    r.Methods(route.Method).Name(route.Name).Handler(handler)
    r.Path(route.Pattern)
    

    instead of mux.Route's Path method (strikethrough'd below)

    r.Methods(route.Method).Path(route.Pattern).Name(route.Name).Handler(handler)

    I am not much familiar with gorilla/mux so couldn't find exact reason behind this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助