douyi1899 2015-07-17 19:59
浏览 20
已采纳

定义了绑定参数但得到404的Goji路线

I've got a Google App Engine app using Goji and defined the following routes:

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)
}

I can GET and POST to /api/list but GETing /api/list/0 just results in a 404, I think from Goji itself.

Does anyone have any idea what I'm doing wrong?

  • 写回答

1条回答 默认 最新

  • doupu1957 2015-07-18 01:26
    关注

    The 404 isn't being returned by Goji—any 404 from Goji should be logged to console (stdout) by the Logger middleware. The upstream handler passing requests to the Goji router is bouncing anything that isn't /api/list.

    You can fix this with a more lenient match:

    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
    ", 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)
    }
    

    Note that I've changed the upstream http.Handle to http.Handle("/api/", mux) to provide a more lenient match. The net/http router is very simple and because /api/list/131313 (e.g. with an id) doesn't match /api/list it 404's before it even hits the Goji mux instance you created.

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理