亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang gorilla mux 未找到處理程序無法正常工作

Golang gorilla mux 未找到處理程序無法正常工作

Go
千萬里不及你 2021-12-07 16:37:56
我正在編寫一個 web 服務器,我有 Not Found Handler 函數。登錄、注冊、查看頁面等所有處理功能均正常工作。我也有這樣的一行:router.NotFoundHandler = http.HandlerFunc(Handlers.NotFoundHandler) Handlers 是我的包,包括下一個代碼:func NotFoundHandler(w http.ResponseWriter, r *http.Request) {    //title has to be 404.html    title := config.Page404    title = config.WebAppex + title    fmt.Println("title: " + title)    /*HERE IS QUESTION LINE*/    r = http.NewRequest("GET", config.Page404, nil)    fmt.Println(r.URL.Path)    logger.Info("Sending... ", title)    //load page    p, err := loadPage(title)    if err != nil {        logger.Error("ERROR : ", err.Error())        p, _ := loadPage(config.Page404)        fmt.Fprintf(w, "%s", p.body) //wired way to send HTML page        return                       //just return because of printing dry p.body    }    //serve it    http.ServeFile(w, r, p.dir)    logger.Info(p.dir, " has been sent")}所以問題是:如果我嘗試訪問 localhost:8080/nothing,那么我得到了很好的 Page404 但是如果我訪問 localhost:8080/nothing/nothing 或 localhost:8080/nothing/nothing/nothing 等等,我會感到枯燥404.html 沒有任何 CSS。所以我認為問題是請求,所以我用“/404.html”(它是config.Page404)的方式創建了新的,但沒有任何改變。我讀過關于 gorilla mux Subrouter 的文章,它可能有助于僅排除 localhost:8080/nothing,但我需要所有情況。那么有沒有辦法排除所有不存在的頁面?UPD:我的 loadPage 功能:func loadPage(title string) (*page, error) {    logger.Info("Trying to load", title)    //writing from title to body    body, err := ioutil.ReadFile(title)    if err != nil {        return nil, err    }    return &page{dir: config.HOMEDIR + title, body: body}, nil}type page struct {    dir  string    body []byte}謝謝!
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

您可能正在使用相對的 CSS 樣式表路徑為 404 頁面提供服務,當從/nothing/nothing/nothing.

要么使用該<base>標簽在任何地方獲取絕對鏈接,要么將您的請求重定向到想要的頁面。


查看完整回答
反對 回復 2021-12-07
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號