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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?