gpt4 book ai didi

go - 使用 JulienSchmidt httprouter 服务 favicon.ico

转载 作者:IT王子 更新时间:2023-10-29 01:47:04 25 4
gpt4 key购买 nike

我可以使用标准的 net/http 包获取 favicon.icon,但在使用 julienschmidt/httprouter 时遇到了问题。这就是我正在尝试的,并且收到 favicon.ico 文件的 404 错误。

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

最佳答案

我能够通过从 stuff/images/favicon.ico 中删除前导斜杠来解决问题。谢谢@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")
}

关于go - 使用 JulienSchmidt httprouter 服务 favicon.ico,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com