golang gorilla/mux设置静态目录

栏目: Go · 发布时间: 7年前

内容简介:发现网上都是类似下面的代码经过mux设置就是如下代码其实这样并不能访问static目录下的文件,包括下载。访问/static/会返回static目录下的文件结构,但是点击的时候回返回404,应该是没有相应的路由,匹配上就可以了:

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

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)))
));

以上所述就是小编给大家介绍的《golang gorilla/mux设置静态目录》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

How to Think About Algorithms

How to Think About Algorithms

Jeff Edmonds / Cambridge University Press / 2008-05-19 / USD 38.99

HOW TO THINK ABOUT ALGORITHMS There are many algorithm texts that provide lots of well-polished code and proofs of correctness. Instead, this one presents insights, notations, and analogies t......一起来看看 《How to Think About Algorithms》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具