GAE Golang Gorilla mux - 未找到404页面 [英] GAE Golang Gorilla mux - 404 page not found

查看:223
本文介绍了GAE Golang Gorilla mux - 未找到404页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我尝试它时,我找不到'404页面'。没有调用rootHandler函数(没有生成痕迹)

下面是我的代码的一部分,有什么想法?



thk

  ... 
func init(){
r:= mux.NewRouter( )
r.HandleFunc(/,rootHandler)
}
func rootHandler(w http.ResponseWriter,r * http.Request){
var functionName =rootHandler
c:= appengine.NewContext(r)
c .fof(functionName + - start)
推迟c.Infof(functionName + - end)
...


解决方案

您必须将请求路由到您的多路复用器路由器。 http 包含 DefaultServeMux ,但 mux 不。 (并且它本身没有用 net / http 注册它的路由)



也就是说,所有你必须做的,将 mux 路由器注册为 net / http



< $ p $ func main(){
r:= mux.NewRouter()
r.HandleFunc(/,HomeHandler)
r.HandleFunc( / products,ProductsHandler)
r.HandleFunc(/ articles,ArticlesHandler)
http.Handle(/,r)
}

(直接从文档)

这里的重要部分是 http.Handle(/,r)


I've got some problems to use gorilla mux within GAE.

When I try it, I've '404 page not found'. The rootHandler function is not called ( no traces generated)

Below is part of my code, any ideas?

thk in advance

...
    func init() {
     r := mux.NewRouter()
     r.HandleFunc("/",rootHandler)
    }
    func rootHandler(w http.ResponseWriter, r *http.Request) {
     var functionName = "rootHandler"
     c := appengine.NewContext(r)
     c.Infof(functionName+"-start")
     defer c.Infof(functionName+"-end")
...

解决方案

You have to route requests to your mux router. http package has DefaultServeMux which is used by AppEngine, but mux does not. (and it's not registering its routes with net/http by itself)

That is, all you have to do, is register your mux router with net/http:

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", HomeHandler)
    r.HandleFunc("/products", ProductsHandler)
    r.HandleFunc("/articles", ArticlesHandler)
    http.Handle("/", r)
}

(straight from the docs)

Important part here is http.Handle("/", r).

这篇关于GAE Golang Gorilla mux - 未找到404页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆