为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 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 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号