在gorilla mux路由器上设置404 NotFound处理程序。

huangapple go评论139阅读模式
英文:

setup 404 NotFound handler on a gorilla mux router

问题

这是我用Go语言和gorilla mux包编写的一个小型演示Web服务器的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gorilla/mux"
  5. "net/http"
  6. )
  7. func handler(w http.ResponseWriter, r *http.Request) {
  8. vars := mux.Vars(r)
  9. fmt.Fprintf(w, "你好,我喜欢%s!", vars["username"])
  10. }
  11. func homeHandler(w http.ResponseWriter, r *http.Request) {
  12. if r.URL.Path != "/" {
  13. errorHandler(w, r, http.StatusNotFound)
  14. return
  15. }
  16. vars := mux.Vars(r)
  17. fmt.Fprintf(w, "你好,我喜欢%s!", vars["username"])
  18. }
  19. func main() {
  20. r := mux.NewRouter()
  21. r.HandleFunc("/help/{username}/", handler)
  22. http.Handle("/", r)
  23. http.ListenAndServe(":8080", nil)
  24. }

但是我找不到如何实现自定义404页面的方法。

但是我不能使用r.HandleFunc("/",...),因为它会太贪婪。

英文:

Here is my code about a small demonstration webserver written with the Go language and the gorilla mux package :

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gorilla/mux"
  5. "net/http"
  6. )
  7. func handler(w http.ResponseWriter, r *http.Request) {
  8. vars := mux.Vars(r)
  9. fmt.Fprintf(w, "Hi there, I love %s!", vars["username"])
  10. }
  11. func homeHandler(w http.ResponseWriter, r *http.Request) {
  12. if r.URL.Path != "/" {
  13. errorHandler(w, r, http.StatusNotFound)
  14. return
  15. }
  16. vars := mux.Vars(r)
  17. fmt.Fprintf(w, "Hi there, I love %s!", vars["username"])
  18. }
  19. func main() {
  20. r := mux.NewRouter()
  21. r.HandleFunc("/help/{username}/", handler)
  22. http.Handle("/", r)
  23. http.ListenAndServe(":8080", nil)
  24. }

But I don't find a way on how to implement a custom 404 page.

But I can't make a r.HandleFunc("/",...) but it will be too greedy.

答案1

得分: 14

Router导出了一个NotFoundHandler字段,你可以将其设置为自定义的处理程序。

  1. r := mux.NewRouter()
  2. r.NotFoundHandler = MyCustom404Handler
英文:

The Router exports a NotFoundHandler field which you can set to your custom handler.

  1. r := mux.NewRouter()
  2. r.NotFoundHandler = MyCustom404Handler

答案2

得分: 11

有时候,你会花费很多时间构建一个中间件堆栈,它可以执行许多任务,比如记录日志、发送指标等等。而默认的404处理程序会跳过所有的中间件。

我通过重新设置默认的404处理程序来解决这个问题,像这样:

  1. router := mux.NewRouter()
  2. router.Use(someMiddleware())
  3. // 重新定义默认的NotFound处理程序
  4. router.NotFoundHandler = router.NewRoute().HandlerFunc(http.NotFound).GetHandler()

现在,404默认处理程序也会经过所有的中间件。

英文:

Sometimes, you spend a lot of time building a stack of middleware that does many things like logging, sending metrics and so... And the default 404 handler just skips all the middlewares.

I was able to solve this issue by re-setting the default 404 handler like this:

  1. router := mux.NewRouter()
  2. router.Use(someMiddleware())
  3. // Re-define the default NotFound handler
  4. router.NotFoundHandler = router.NewRoute().HandlerFunc(http.NotFound).GetHandler()

Now, the 404 default handler is also going thru all the middlewares.

答案3

得分: 2

NotFoundHandler设置为一个处理程序方法,该方法返回您自定义的404页面。

英文:

Set the NotFoundHandler to a handler method that returns your custom 404 page.

答案4

得分: 1

你好!以下是你要翻译的内容:

  1. r := mux.NewRouter()
  2. h := http.HandlerFunc(NotFound)
  3. r.NotFoundHandler = h
  4. func NotFound(w http.ResponseWriter, r *http.Request) {
  5. }

翻译结果:

  1. r := mux.NewRouter()
  2. h := http.HandlerFunc(NotFound)
  3. r.NotFoundHandler = h
  4. func NotFound(w http.ResponseWriter, r *http.Request) {
  5. }

希望对你有帮助!如果还有其他问题,请随时提问。

英文:
  1. r := mux.NewRouter()
  2. h := http.HandlerFunc(NotFound)
  3. r.NotFoundHandler = h
  4. func NotFound(w http.ResponseWriter, r *http.Request) {
  5. }

huangapple
  • 本文由 发表于 2017年4月25日 22:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/43613311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定