dsp15140275697 2016-01-28 19:01
浏览 45
已采纳

找不到Golang大猩猩Mux处理程序无法正常工作

I'm writing a web server and I've got Not Found Handler function. All the Handle functions like login, registration, view page are working correctly. I also have such line: router.NotFoundHandler = http.HandlerFunc(Handlers.NotFoundHandler) Handlers is my package including next code:

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")
}

So the problem is: If I try going to localhost:8080/nothing, then I get my nice Page404 But if I go to localhost:8080/nothing/nothing or localhost:8080/nothing/nothing/nothing and etc, I get dry 404.html without any CSS. So I thought that problem is request, so I create new with the way of "/404.html" (it's config.Page404), but nothing changed. I read about gorilla mux Subrouter, bit It may help to exclude only localhost:8080/nothing, but I need all the cases. So is there any ways to exclude all the not existing pages?

UPD: My loadPage function:

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
}

Thank you!

  • 写回答

1条回答 默认 最新

  • duan1983 2016-01-28 19:41
    关注

    You're probably serving your 404 page with a relative CSS stylesheet path, which will obviously fail when serving it from a nested page like /nothing/nothing/nothing.

    Either use the <base> tag to get absolute links everywhere, or redirect your request to the wanted page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料