问题描述
我使用Gorilla mux
作为我的路由器,我的行为非常奇怪。在对服务器的第一次请求中,我得到了有效的响应。但在随后的请求中,我收到一个 404页面未找到
。
我的代码非常简单(可以复制粘贴来测试它):
软件包主要
导入(
fmt
github.com/gorilla/mux
log
net / http
)
func main(){
router:= mux.NewRouter()
router。 HandleFunc(/,RootHandler).Name(root)
http.Handle(/,路由器)
log.Println(监听端口1337 ... )
if err:= http.ListenAndServe(:1337,nil); err!= nil {
log.Fatal(http.ListenAndServe:,err)
}
}
func RootHandler(w http.ResponseWriter,r * http.Request){
content:=Welcome to
rootUrl,err:= mux.CurrentRoute(r).Subrouter()。Get(root)。URL()
如果err!= nil {
log.Printf(mux.CurrentRoute(r).Subrouter()。Get(\root \)。URL():,err)
}
response:= content + rootUrl.String()
fmt.Fprintf(w,response)
}
经过一些代码评论和测试后,似乎下面一行是罪魁祸首:
rootUrl,err:= mux.CurrentRoute(r).Subrouter()。Get(root)。URL()
使用当前请求在处理程序中获取路由器的这种方法来自另一个StackOverflow帖子:
但是出于一个奇怪的原因,它只能使用一次:
shell-1 $ go run servertest.go
2014/10/30 13:31:34监听端口1337 ...
shell-2 $ curl http ://127.0.0.1:1337
欢迎使用/
shell-2 $ curl http://127.0.0.1:1337
404页未找到
正如您所看到的,控制台中没有错误。
想知道为什么它只能运作一次 ?
问题是Subrouter()不会返回路由器,而是创建一个,因此它会更改所调用路由器的匹配器,从而使您失去处理程序。
func RootHandler(router * mux.Router)func (http.ResponseWriter,* http.Request){
return func(w http.ResponseWriter,r * http.Request){
...
}
}
I'm using Gorilla mux
as my router and I'm having a very strange behaviour. On the first request to the server, I get a valid response. But on subsequent requests, I receive a 404 page not found
. There are no errors in the console.
My code is pretty straightforward (it can be copy-pasted to test it right out):
package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/", RootHandler).Name("root")
http.Handle("/", router)
log.Println("Listening on port 1337...")
if err := http.ListenAndServe(":1337", nil); err != nil {
log.Fatal("http.ListenAndServe: ", err)
}
}
func RootHandler(w http.ResponseWriter, r *http.Request) {
content := "Welcome to "
rootUrl, err := mux.CurrentRoute(r).Subrouter().Get("root").URL()
if err != nil {
log.Printf("mux.CurrentRoute(r).Subrouter().Get(\"root\").URL(): ", err)
}
response := content + rootUrl.String()
fmt.Fprintf(w, response)
}
After some code commenting and tests, it seems the following line is the culprit:
rootUrl, err := mux.CurrentRoute(r).Subrouter().Get("root").URL()
This method of getting the router inside the handler using the current request comes from another StackOverflow post: How to call a route by its name from inside a handler?
But for a strange reason, it only works once:
shell-1$ go run servertest.go
2014/10/30 13:31:34 Listening on port 1337...
shell-2$ curl http://127.0.0.1:1337
Welcome to /
shell-2$ curl http://127.0.0.1:1337
404 page not found
As you can see, there are no errors in the console.
Does someone have an idea of why it only works once ?
The problem is Subrouter() isn't made to return the router, but to create one, thus it changes the matcher of the router it is called on, making you lose the handler.
You could try passing the router to the handler using closures instead.
func RootHandler(router *mux.Router) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
...
}
}
这篇关于来自内部处理程序的Gorilla Mux路由器只能工作一次,然后找不到404页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!