如何使用Gorilla mux正确提供单个静态文件?

6
我看了这个问题Serving static content with a root URL with the Gorilla toolkit,并成功地使用了那里的答案。
然而,为了开发目的,我想明确声明我要作为静态文件提供服务的文件,像这样:
router.PathPrefix("/style.css").Handler(http.FileServer(http.Dir("/usr/local/myproject/style.css")))

这也按预期工作。

现在,我想知道,是否以这种方式提供单个明确定义的文件是正确的,或者我应该采取不同的方法?


当您访问类似于<url>/style.css/abc这样的内容时,您会得到什么响应? - Ankur
1
我猜你应该使用 Path 路由器,而不是使用 PathPrefix - Ankur
6
为提供单个文件,更轻量/更快的选择是使用http.ServeFile()。详见以下答案:在Go模板中引入js文件 - icza
1个回答

0

您可以直接使用http.ServeFile()函数:

http.ServeFile(w, r, "/usr/local/myproject/style.css")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接