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

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

httpRouter 服務靜態文件的問題

httpRouter 服務靜態文件的問題

Go
慕少森 2022-12-13 16:34:01
我搜索了現有的線程并按照他們所說的做了沒有成功。當我在 localhost:8081/ 訪問我的 go 應用程序時,它成功加載了 index.html 但它沒有找到 js 文件,因此沒有顯示任何內容,我正在嘗試使用 go 服務一個反應應用程序。問題出在 ServeFiles 函數上,因為它無法正常工作。func RouteDefault(w http.ResponseWriter, r *http.Request, p httprouter.Params) {    index := template.Must(template.ParseFiles("static/index.html"))    index.Execute(w, nil)}func main() {    // BasicAuth username and password    user := ""    pass := ""    DefaultUser()    // HTTPRouter Settings and Routes    router := httprouter.New()    router.ServeFiles("/static/*filepath", http.Dir("static"))    router.GET("/", RouteDefault)    router.GET("/dashboard/", RouteDefault)    router.GET("/about/", RouteDefault)    router.GET("/signout/", RouteDefault)    router.POST("/login/", BasicAuth(RouteLogin, user, pass))    router.GET("/getusers/", JWTAuth(RouteGetUsers))    router.POST("/newuser/", JWTAuth(RouteNewUser))    router.POST("/deleteuser/", JWTAuth(RouteDeleteUser))    router.POST("/mypassword/", JWTAuth(RouteMyPassword))    router.POST("/upload/", JWTAuth(RouteUpload))    router.GET("/getgraphs/", JWTAuth(RouteGetGraphs))    router.POST("/graph/", JWTAuth(RouteGetGraph))    router.POST("/deletegraph/", JWTAuth(RouteDeleteGraph))    router.GET("/autologin/", JWTAuth(RouteAutoLogin))    handler := cors.AllowAll().Handler(router)    fmt.Println(http.ListenAndServe(":8081", handler))}
查看完整描述

1 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

參考這個github鏈接:https://github.com/julienschmidt/httprouter/issues/7

我添加了第二個 httprouter 實例,并通過它提供靜態文件,然后將第一個 httprouter NotFound 設置為第二個 httprouter

static := httprouter.New()
static.ServeFiles("/*filepath", http.Dir("static"))
router.NotFound = static


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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