仅提供选定的文件夹
Serving selected folders only
我遇到了 Go 静态内容网站问题。
我的结构如下所示:
|-Go
|--static/*
|--go.mod
|--main.go
当我以这种方式提供静态内容时
router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
http.FileServer(http.Dir("./")).ServeHTTP(res, req)
})
我可以在urlhttps://example.com/go.mod
中看到go.mod的内容
然后我把代码改成了
router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
http.FileServer(http.Dir("./static/*")).ServeHTTP(res, req)
})
我也试过了
router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
http.FileServer(http.Dir("./static/")).ServeHTTP(res, req)
})
我在 url https://example.com/go.mod
中再也看不到 go.mod 的内容了
但是我的标志不再可见了<img src="/static/imgs/logo.png" alt="logo" id="logo"/>
如何只提供 ./static/
目录的内容?
到静态目录下的服务器内容:
http.FileServer(http.Dir("./static/"))
对 /imgs/logo.png 的请求将是来自 ./static/imgs/logo.png 的服务器,(不是 /static/img/logo.png)
如果您还想在网址中包含 /static/:
http.StripPrefix("/static/",http.FileServer(http.Dir("./static/")))
我遇到了 Go 静态内容网站问题。 我的结构如下所示:
|-Go
|--static/*
|--go.mod
|--main.go
当我以这种方式提供静态内容时
router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
http.FileServer(http.Dir("./")).ServeHTTP(res, req)
})
我可以在urlhttps://example.com/go.mod
中看到go.mod的内容然后我把代码改成了
router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
http.FileServer(http.Dir("./static/*")).ServeHTTP(res, req)
})
我也试过了
router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
http.FileServer(http.Dir("./static/")).ServeHTTP(res, req)
})
我在 url https://example.com/go.mod
中再也看不到 go.mod 的内容了但是我的标志不再可见了<img src="/static/imgs/logo.png" alt="logo" id="logo"/>
如何只提供 ./static/
目录的内容?
到静态目录下的服务器内容:
http.FileServer(http.Dir("./static/"))
对 /imgs/logo.png 的请求将是来自 ./static/imgs/logo.png 的服务器,(不是 /static/img/logo.png)
如果您还想在网址中包含 /static/:
http.StripPrefix("/static/",http.FileServer(http.Dir("./static/")))