1 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
將Router在MUX庫(kù)類型有一個(gè)NotFoundHandler類型的字段http.Handler。這將允許您按照您認(rèn)為合適的方式處理不匹配的路線:
// NotFoundHandler overrides the default not found handler
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
// You can use the serve file helper to respond to 404 with
// your request file.
http.ServeFile(w, r, "public/index.html")
}
func main() {
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
// Register other routes or setup negroni
log.Fatal(http.ListenAndServe(":8080", r))
}
- 1 回答
- 0 關(guān)注
- 178 瀏覽
添加回答
舉報(bào)