英文:
Defined a Goji route with bound parameter but getting 404
问题
我有一个使用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,但是GET请求/api/list/0只会导致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
得分: 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)
// 如果Goji的路由器返回404,我们应该调用我们的自定义处理程序,并且它也应该在日志中显示出来。
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
路由器非常简单,因此,因为/api/list/131313
(例如,带有id)与/api/list
不匹配,它在甚至未触及您创建的Goji mux实例之前就返回了404。
希望对您有所帮助。
英文:
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\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)
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论