定义了具有绑定参数的Goji路由,但获得了404

时间:2015-07-17 19:59:24

标签: google-app-engine go goji

我使用Goji获得了一个Google App Engine应用,并定义了以下路线:

func init() {
    mux := web.New()
    http.Handle("/api/list", mux)

    mux.Use(middleware.EnvInit)
    mux.Use(middleware.Logger)

    mux.Get( "/api/list",       list.HandleListGetAll)
    mux.Post("/api/list",       list.HandleListNewList)
    mux.Get( "/api/list/:id",   list.HandleListGetSingle)
}

我可以GET和POST到/ api / list,但GETing / api / list / 0只会产生404,我认为是Goji本身。

有没有人知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

Goji不会返回404 - 来自Goji的任何404都应该被select t1.*, t2.* from (select t.*, (@rn := @rn + 1) as seqnum from table1 t cross join (select @rn := 0) params order by t.id ) t1 join (select t.*, (@rn2 := @rn2 + 1) as seqnum from table2 t cross join (select @rn2 := 0) params order by t2.id ) t2 on t1.seqnum = t2.seqnum; 中间件记录到控制台(stdout)。将请求传递给Goji路由器的上游处理程序正在弹出不是Logger的任何内容。

您可以通过更宽松的匹配来解决此问题:

/api/list

请注意,我已将上游package main import ( "fmt" "net/http" "github.com/zenazn/goji/web" "github.com/zenazn/goji/web/middleware" ) func main() { mux := web.New() http.Handle("/api/", mux) mux.Use(middleware.EnvInit) mux.Use(middleware.Logger) mux.Get("/api/list/:id", debugHandler) mux.Get("/api/list", debugHandler) mux.Post("/api/list", debugHandler) // If Goji's router 404's, we should call our custom handler, and it // should also be apparent in the logs. mux.NotFound(notFoundHandler) http.ListenAndServe(":8000", nil) } func debugHandler(c web.C, w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%v\n", r.URL.Path) } func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("Goji 404: %s", r.URL.Path), 404) } 更改为http.Handle以提供更宽松的匹配。 http.Handle("/api/", mux)路由器非常简单,因为net/http(例如带有ID)在它遇到您创建的Goji mux实例之前不会与/api/list/131313匹配/api/list

希望有所帮助。