douxie3625 2015-02-03 07:25
浏览 113
已采纳

找不到404页面-渲染CSS文件

I'm currently working in Go. I created a web server on my local machine. I followed the instruction on this page Rendering CSS in a Go Web Application but I'm still getting the 404 error that the program can't seem to find where my css file is. My directory is as follows.

In src folder contains css/somefilename.css, src also contains server/server.go.

The code inside my server.go file is as follows.

http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))

When I go to localhost:8080/css/ I get 404 page not found. I'm also using templates to render the html code. The templates are in the folder

src/templates/layout.html

the html code is as follows:

<link rel="stylesheet" type="text/css" href="../css/css490.css" />
  • 写回答

1条回答 默认 最新

  • dongtaotao19830418 2015-02-03 08:38
    关注

    Since you don't specify full path for the css folder just a relative one, whether your css files are found depends on the folder you run your application from (the working directory, this is what relative paths are resolved to).

    For example if you start your application from your src with go run server/server.go it will work. If you start it from your src/server folder with go run server.go, it will not work. Also if you create a native executable from your app which is put into the bin folder and you start that from the bin folder, this also won't work because the css folder is not in the bin folder.

    Either start it with go run server/server.go from the src folder, or copy the css folder to your bin folder and start the executable from the bin folder and it should work (but in this case you also have to copy other static files like html templates).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现