【发布时间】:2015-04-02 07:50:13
【问题描述】:
我目前正在使用 Go。我在本地机器上创建了一个 Web 服务器。我按照此页面上的说明进行操作Rendering CSS in a Go Web Application 但我仍然收到程序似乎无法找到我的 css 文件所在位置的 404 错误。我的目录如下。
在src文件夹中包含css/somefilename.css,src也包含server/server.go。
我的server.go文件里面的代码如下。
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
当我转到localhost:8080/css/ 时,我得到 404 页面未找到。我还使用模板来呈现 html 代码。模板在文件夹中
src/templates/layout.html
html代码如下:
<link rel="stylesheet" type="text/css" href="../css/css490.css" />
【问题讨论】:
-
如果将
css文件夹的完整路径放在http.FileServer(http.Dir("/full/path/of/css")中会更好吗? -
是的,当我使用完整路径时,它按预期工作。我唯一的抱怨是为什么它可以在我的朋友机器(mac)上使用上面的代码但是当我在我的机器(windows)上运行它时它不起作用。我们实际上有相同的 css 文件到 server.go 的文件路径
-
仅用于测试,将
"css"替换为filepath.FromSlash(path.Clean("css")会更好(并且适用于任何平台)?
标签: html css go http-status-code-404 go-templates