我正在尝试使用Gorilla工具包的mux
包在Go Web服务器中路由URL。 参考这个问题,我有以下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>
...
当我在浏览器中访问http://localhost:8100
时,成功获取到index.html
内容,但所有的js
和css
URL都返回404错误。
我该如何使程序能够提供位于static
子目录下的文件?
main.go
旁边)有任何静态文件。此外,它似乎与下面的@Joe's answer非常相似,这也无法在我的设置中工作。 - jason