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" />
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
withgo run server/server.go
it will work. If you start it from yoursrc/server
folder withgo run server.go
, it will not work. Also if you create a native executable from your app which is put into thebin
folder and you start that from thebin
folder, this also won't work because thecss
folder is not in thebin
folder.Either start it with
go run server/server.go
from thesrc
folder, or copy thecss
folder to yourbin
folder and start the executable from thebin
folder and it should work (but in this case you also have to copy other static files like html templates).