douweiluo0600 2018-03-29 15:18 采纳率: 0%
浏览 62
已采纳

使用JulienSchmidt httprouter服务favicon.ico

I can get favicon.icon with the standard net/http package but am having trouble with julienschmidt/httprouter. This is what I'm trying and am receiving a 404 error for the favicon.ico file.

import (
    "github.com/julienschmidt/httprouter"
    "net/http"
    "log"
)

func main(){
    router := httprouter.New()
    router.GET("/", index)
    router.POST("/", login)
    router.GET("/logout", logout)
    router.GET("/favicon.ico", faviconHandler)
    router.ServeFiles("/stuff/*filepath", http.Dir("stuff"))
    log.Fatal(http.ListenAndServe(":8080", router))
}

func faviconHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
     http.ServeFile(w, r, "/stuff/images/favicon.ico")
}
  • 写回答

1条回答 默认 最新

  • dsjklb0205 2018-03-29 16:56
    关注

    I was able to solve the problem by removing the leading slash from stuff/images/favicon.ico. Thanks @Peter.

    import (
        "github.com/julienschmidt/httprouter"
        "net/http"
        "log"
    )
    
    func main(){
        router := httprouter.New()
        router.GET("/", index)
        router.POST("/", login)
        router.GET("/logout", logout)
        router.GET("/favicon.ico", faviconHandler)
        router.ServeFiles("/stuff/*filepath", http.Dir("stuff"))
        log.Fatal(http.ListenAndServe(":8080", router))
    }
    
    func faviconHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
         http.ServeFile(w, r, "stuff/images/favicon.ico")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示
  • ¥15 关于#C语言#的问题,请各位专家解答!(语言-c语言)
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题