Golang

golang gorilla/mux设置静态目录

2018-12-28  本文已影响40人  鹅鹅鹅_

发现网上都是类似下面的代码

s :=   "/Users/golang/golang";
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir(s))))

经过mux设置就是如下代码




router := mux.NewRouter().StrictSlash(true)

s :=   "/Users/golang/golang";

router.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir(s))))

其实这样并不能访问static目录下的文件,包括下载。访问/static/会返回static目录下的文件结构,但是点击的时候回返回404,应该是没有相应的路由,匹配上就可以了:


router.PathPrefix("/static/").Handler(http.StripPrefix("/static/",http.FileServer(http.Dir(s)))
));

上一篇 下一篇

猜你喜欢

热点阅读