【发布时间】:2013-03-27 21:20:26
【问题描述】:
我正在尝试使用 Gorilla 工具包的 mux package 在 Go Web 服务器中路由 URL。使用 this question 作为指导,我有以下 Go 代码:
func main() {
r := mux.NewRouter()
r.Handle("/", http.FileServer(http.Dir("./static/")))
r.HandleFunc("/search/{searchTerm}", Search)
r.HandleFunc("/load/{dataId}", Load)
http.Handle("/", r)
http.ListenAndServe(":8100", nil)
}
目录结构为:
...
main.go
static\
| index.html
| js\
| <js files>
| css\
| <css files>
Javascript 和 CSS 文件在 index.html 中引用如下:
...
<link rel="stylesheet" href="css/redmond/jquery-ui.min.css"/>
<script src="js/jquery.min.js"></script>
...
当我在 Web 浏览器中访问 http://localhost:8100 时,index.html 内容已成功交付,但是,所有 js 和 css URL 都返回 404。
如何让程序从static 子目录中提供文件?
【问题讨论】:
-
您可能希望看到有关从根目录或子目录提供静态文件stackoverflow.com/questions/14086063/… 的讨论(尽管不使用 Gorilla)
-
@Ripounet,我在研究过程中确实看到了这个问题,但是,由于它没有使用 Gorilla,所以我永远无法获得与我的目标之一没有的设置一起使用的想法我项目根目录中的所有静态文件(
main.go旁边)。此外,它似乎与下面的@Joe's answer 非常相似,这也不适用于我的设置。
标签: web-applications go mux