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

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

setup 404 NotFound handler on a gorilla mux router

问题

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

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	fmt.Fprintf(w, "你好,我喜欢%s!", vars["username"])
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		errorHandler(w, r, http.StatusNotFound)
		return
	}
	vars := mux.Vars(r)
	fmt.Fprintf(w, "你好,我喜欢%s!", vars["username"])
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/help/{username}/", handler)
	http.Handle("/", r)
	http.ListenAndServe(":8080", nil)
}

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

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

英文:

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

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	fmt.Fprintf(w, "Hi there, I love %s!", vars["username"])
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		errorHandler(w, r, http.StatusNotFound)
		return
	}
	vars := mux.Vars(r)
	fmt.Fprintf(w, "Hi there, I love %s!", vars["username"])
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/help/{username}/", handler)
	http.Handle("/", r)
	http.ListenAndServe(":8080", nil)
}

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字段,你可以将其设置为自定义的处理程序。

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

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

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

答案2

得分: 11

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

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

router := mux.NewRouter()
router.Use(someMiddleware())
// 重新定义默认的NotFound处理程序
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:

router := mux.NewRouter()
router.Use(someMiddleware())
// Re-define the default NotFound handler
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

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

r := mux.NewRouter()

h := http.HandlerFunc(NotFound)
r.NotFoundHandler = h

func NotFound(w http.ResponseWriter, r *http.Request) {

}

翻译结果:

r := mux.NewRouter()

h := http.HandlerFunc(NotFound)
r.NotFoundHandler = h

func NotFound(w http.ResponseWriter, r *http.Request) {

}

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

英文:
r := mux.NewRouter()

h := http.HandlerFunc(NotFound)
r.NotFoundHandler = h
    
func NotFound(w http.ResponseWriter, r *http.Request) {
    	
}

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:

确定