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

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

如何使用 gorilla/mux 在 GO 中將我的 bundle.js 文件與我的 html

如何使用 gorilla/mux 在 GO 中將我的 bundle.js 文件與我的 html

Go
陪伴而非守候 2023-03-21 13:17:19
我正在嘗試制作一個帶有 Go 后端和 React 前端的簡單服務器。為此,我需要發送我的靜態 html 和 bundle.js 文件。她的 html<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <link rel="stylesheet" href="https://cdn.rawgit.com/twbs/bootstrap/48938155eb24b4ccdde09426066869504c6dab3c/dist/css/bootstrap.min.css">     <title>Mern Gen</title>  </head>  <body><main id='root'>  App has crashed</main><script src="../public/bundle.js" type="text/javascript"></script></body></html>目前我這樣做是為了將兩個文件都傳送到 '/' urlbs := http.FileServer(http.Dir("public"))http.Handle("/public/", http.StripPrefix("/public/", bs))fs := http.FileServer(http.Dir("./static"))http.Handle("/", fs)我現在需要像這樣使用 gorilla/mux 來匹配可變參數r.HandleFunc("/loc/{id}", getLoc)但如果我這樣做,我還必須從默認的多路復用器更改為大猩猩路由器r := mux.NewRouter()bs := http.FileServer(http.Dir("public"))r.Handle("/public/", http.StripPrefix("/public/", bs))fs := http.FileServer(http.Dir("./static"))r.Handle("/", fs)這是行不通的。我收到一條錯誤消息,指出找不到我的 bundle.js。我如何使用大猩猩復用器做到這一點?
查看完整描述

1 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

您應該使用目錄PathPrefix中的服務文件public:


r := mux.NewRouter()


bs := http.FileServer(http.Dir("public"))

r.PathPrefix("/public/").Handler(http.StripPrefix("/public/", bs))


fs := http.FileServer(http.Dir("./static"))

r.Handle("/", fs)


http.Handle("/", r)


查看完整回答
反對 回復 2023-03-21
  • 1 回答
  • 0 關注
  • 69 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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