我正在玩一个小玩具服务器来学习围棋网络编程。
我的项目目录结构有以下 public
目录:
public\
| style.css
public
和style.css
的权限是r-x
和r--
给所有人。
在 main.go
中,我有以下几行:
router := mux.NewRouter()
router.Handle("/static/",
http.StripPrefix("/static/", http.FileServer(http.Dir("public"))))
log.Fatal(http.ListenAndServe(":3001", router))
每次调用http://localhost:3001/static/style.css
服务器返回 404。
我已经尝试了路径中前导斜线和尾随斜线的所有组合,但没有任何区别。
我在 Ubuntu 15.10 (x64) 上运行 Go v1.5.3。
最佳答案
这是一个示例,说明如何从名为 public
的文件夹中向 /static/
中的文件提供任何请求。
router := mux.NewRouter()
//router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("public"))))
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("public/"))))
log.Fatal(http.ListenAndServe(":3001", router))
关于go - 无法在 Go Gorilla 服务器中提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34830888/