0

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

/api/list に対して GET および POST を実行できますが、/api/list/0 を GET すると 404 になるだけです。Goji 自体からだと思います。

誰かが私が間違っていることを知っていますか?

4

1 に答える 1

1

404 は Goji によって返されません。Goji からの 404 はすべて、Loggerミドルウェアによってコンソール (stdout) に記録される必要があります。要求を Goji ルーターに渡すアップストリーム ハンドラーが、そうでないものをバウンスしてい/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をに変更したことに注意してください。ルーターは非常に単純で、作成した Goji マルチプレクサー インスタンスに到達する前に 404 と一致しないためhttp.Handle("/api/", mux)( IDなど) 。net/http/api/list/131313/api/list

それが役立つことを願っています。

于 2015-07-18T01:26:08.333 に答える