第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

顯示帶有標(biāo)準(zhǔn)http包的自定義404錯(cuò)誤頁面

顯示帶有標(biāo)準(zhǔn)http包的自定義404錯(cuò)誤頁面

Go
楊__羊羊 2021-05-05 16:12:29
假設(shè)我們有:http.HandleFunc("/smth", smthPage)http.HandleFunc("/", homePage)嘗試輸入錯(cuò)誤的URL時(shí),用戶會(huì)看到一個(gè)普通的“找不到404頁”。如何為這種情況返回自定義頁面?有關(guān)大猩猩/木糖的更新對(duì)于使用純net / http軟件包的用戶,可以接受答案。如果您使用大猩猩/多路復(fù)用器,則應(yīng)使用以下內(nèi)容:func main() {    r := mux.NewRouter()    r.NotFoundHandler = http.HandlerFunc(notFound)}并func notFound(w http.ResponseWriter, r *http.Request)根據(jù)需要實(shí)施。
查看完整描述

3 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

我通常這樣做:


package main


import (

    "fmt"

    "net/http"

)


func main() {

    http.HandleFunc("/", homeHandler)

    http.HandleFunc("/smth/", smthHandler)

    http.ListenAndServe(":12345", nil)

}


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

    if r.URL.Path != "/" {

        errorHandler(w, r, http.StatusNotFound)

        return

    }

    fmt.Fprint(w, "welcome home")

}


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

    if r.URL.Path != "/smth/" {

        errorHandler(w, r, http.StatusNotFound)

        return

    }

    fmt.Fprint(w, "welcome smth")

}


func errorHandler(w http.ResponseWriter, r *http.Request, status int) {

    w.WriteHeader(status)

    if status == http.StatusNotFound {

        fmt.Fprint(w, "custom 404")

    }

}

在這里,我將代碼簡(jiǎn)化為僅顯示自定義404,但實(shí)際上我使用此設(shè)置做了更多操作:我使用來處理所有HTTP錯(cuò)誤errorHandler,在其中記錄有用的信息并向自己發(fā)送電子郵件。


查看完整回答
反對(duì) 回復(fù) 2021-05-10
?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

以下是我選擇的方法。它基于一個(gè)代碼片段,由于我丟失了瀏覽器書簽,因此我無法對(duì)其進(jìn)行確認(rèn)。


示例代碼:(我將其放在主程序包中)


type hijack404 struct {

    http.ResponseWriter

    R *http.Request

    Handle404 func (w http.ResponseWriter, r *http.Request) bool

}


func (h *hijack404) WriteHeader(code int) {

    if 404 == code && h.Handle404(h.ResponseWriter, h.R) {

        panic(h)

    }


    h.ResponseWriter.WriteHeader(code)

}


func Handle404(handler http.Handler, handle404 func (w http.ResponseWriter, r *http.Request) bool) http.Handler {

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

        hijack := &hijack404{ ResponseWriter:w, R: r, Handle404: handle404 }


        defer func() {

            if p:=recover(); p!=nil {

                if p==hijack {

                    return

                }

                panic(p)

            }

        }()


        handler.ServeHTTP(hijack, r)

    })

}


func fire404(res http.ResponseWriter, req *http.Request) bool{

    fmt.Fprintf(res, "File not found. Please check to see if your URL is correct.");


    return true;

}


func main(){

    handler_statics := http.StripPrefix("/static/", http.FileServer(http.Dir("/Path_To_My_Static_Files")));


    var v_blessed_handler_statics http.Handler = Handle404(handler_statics, fire404);


    http.Handle("/static/", v_blessed_handler_statics);


    // add other handlers using http.Handle() as necessary


    if err := http.ListenAndServe(":8080", nil); err != nil{

        log.Fatal("ListenAndServe: ", err);

    }

}

請(qǐng)自定義,func fire404以輸出您自己的錯(cuò)誤消息版本404。


如果您碰巧正在使用Gorilla Mux,則不妨將主要功能替換為以下內(nèi)容:


func main(){

    handler_statics := http.StripPrefix("/static/", http.FileServer(http.Dir("/Path_To_My_Static_Files")));


    var v_blessed_handler_statics http.Handler = Handle404(handler_statics, fire404);


    r := mux.NewRouter();

    r.PathPrefix("/static/").Handler(v_blessed_handler_statics);


    // add other handlers with r.HandleFunc() if necessary...


    http.Handle("/", r);


    log.Fatal(http.ListenAndServe(":8080", nil));

}

如果錯(cuò)誤,請(qǐng)更正代碼,因?yàn)槲抑皇荊o的新手。謝謝。


查看完整回答
反對(duì) 回復(fù) 2021-05-10
  • 3 回答
  • 0 關(guān)注
  • 292 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)