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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失