gpt4 book ai didi

html - 在 http.HandlerFunc 中调用 http.FileServer

转载 作者:数据小太阳 更新时间:2023-10-29 03:14:13 26 4
gpt4 key购买 nike

所以我最近遇到了一些麻烦......这是我的代码:

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

我的问题:我正在尝试提供文件夹中的一些 HTML 文件:./docs/html。我的文件夹结构:

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

您会在要点中注意到,我在 http.HandlerFunc ServeDocs 上调用了 ServeHTTP 方法,然后通过路由器(mux.路由器)。我遇到的问题是由于某种原因,在 localhost:8080/ 提供的唯一文件是 index.html,当我导航到 localhost 时: 8080/rest.html 我收到 404。

真正奇怪的是,当我删除所有路由器代码并执行如下操作时:

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

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

一切正常。有人知道发生了什么事吗?我花了几个小时试图解决这个问题,但我终于放弃了。

最佳答案

如果你使用 mux.RouterPath 方法,它会起作用

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

代替 mux.RoutePath 方法(下面有删除线)

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

我对 gorilla/mux 不太熟悉,所以找不到这背后的确切原因。

关于html - 在 http.HandlerFunc 中调用 http.FileServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41774588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com