httprouter 配置 NotFound
httprouter configuring NotFound
我正在使用 httprouter
for an API and I'm trying to work out how to handle 404s. It does say in the docs 可以手动处理 404,但我真的不知道如何编写自己的 自定义处理程序.
我在其他路线之后尝试了以下...
router.NotFound(pageNotFound)
但是我收到错误 not enough arguments in call to router.NotFound
。
如果有人能指出我正确的方向,那就太好了。
类型 httprouter.Router
是一个 struct
,它有一个字段:
NotFound http.Handler
所以 NotFound
的类型是 http.Handler
,一个只有一个方法的接口类型:
ServeHTTP(ResponseWriter, *Request)
如果您想要自己的自定义 "Not Found" 处理程序,您必须设置一个实现此接口的值。
最简单的方法是定义一个带签名的函数:
func(http.ResponseWriter, *http.Request)
并使用 http.HandlerFunc()
辅助函数将其 "convert" 为实现 http.Handler
接口的值,其 ServeHTTP()
方法仅调用具有上述签名的函数.
例如:
func MyNotFound(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusNotFound) // StatusNotFound = 404
w.Write([]byte("My own Not Found handler."))
w.Write([]byte(" The page you requested could not be found."))
}
var router *httprouter.Router = ... // Your router value
router.NotFound = http.HandlerFunc(MyNotFound)
此 NotFound
处理程序将由 httprouter
程序包调用。如果您想从其他处理程序之一手动调用它,则必须将 ResponseWriter
and a *Request
传递给它,如下所示:
func ResourceHandler(w http.ResponseWriter, r *http.Request) {
exists := ... // Find out if requested resource is valid and available
if !exists {
MyNotFound(w, r) // Pass ResponseWriter and Request
// Or via the Router:
// router.NotFound(w, r)
return
}
// Resource exists, serve it
// ...
}
NotFound 是 http.HandlerFunc 类型的函数指针。您必须将签名为 Func(w ResponseWriter, r *Request)
的函数放在某处并将其分配给 NotFound (router.NotFound = Func
).
我正在使用 httprouter
for an API and I'm trying to work out how to handle 404s. It does say in the docs 可以手动处理 404,但我真的不知道如何编写自己的 自定义处理程序.
我在其他路线之后尝试了以下...
router.NotFound(pageNotFound)
但是我收到错误 not enough arguments in call to router.NotFound
。
如果有人能指出我正确的方向,那就太好了。
类型 httprouter.Router
是一个 struct
,它有一个字段:
NotFound http.Handler
所以 NotFound
的类型是 http.Handler
,一个只有一个方法的接口类型:
ServeHTTP(ResponseWriter, *Request)
如果您想要自己的自定义 "Not Found" 处理程序,您必须设置一个实现此接口的值。
最简单的方法是定义一个带签名的函数:
func(http.ResponseWriter, *http.Request)
并使用 http.HandlerFunc()
辅助函数将其 "convert" 为实现 http.Handler
接口的值,其 ServeHTTP()
方法仅调用具有上述签名的函数.
例如:
func MyNotFound(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusNotFound) // StatusNotFound = 404
w.Write([]byte("My own Not Found handler."))
w.Write([]byte(" The page you requested could not be found."))
}
var router *httprouter.Router = ... // Your router value
router.NotFound = http.HandlerFunc(MyNotFound)
此 NotFound
处理程序将由 httprouter
程序包调用。如果您想从其他处理程序之一手动调用它,则必须将 ResponseWriter
and a *Request
传递给它,如下所示:
func ResourceHandler(w http.ResponseWriter, r *http.Request) {
exists := ... // Find out if requested resource is valid and available
if !exists {
MyNotFound(w, r) // Pass ResponseWriter and Request
// Or via the Router:
// router.NotFound(w, r)
return
}
// Resource exists, serve it
// ...
}
NotFound 是 http.HandlerFunc 类型的函数指针。您必须将签名为 Func(w ResponseWriter, r *Request)
的函数放在某处并将其分配给 NotFound (router.NotFound = Func
).