gpt4 book ai didi

go - 无法在 Go Gorilla 服务器中提供静态文件

转载 作者:IT王子 更新时间:2023-10-29 02:29:52 26 4
gpt4 key购买 nike

我正在玩一个小玩具服务器来学习围棋网络编程。

我的项目目录结构有以下 public 目录:

public\
| style.css

publicstyle.css 的权限是r-xr-- 给所有人。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com