404页面未找到 - 转换渲染css文件 [英] 404 page not found - Go rendering css file
问题描述
我目前在Go工作。我在我的本地机器上创建了一个Web服务器。我按照此页面上的说明在Go Web应用程序中渲染CSS
但我仍然得到404错误,程序似乎找不到我的css文件是什么。我的目录如下。
在 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
pre>
html代码如下:
< link rel =stylesheettype =text / csshref =../ css / css490.css/>
解决方案code> css 文件夹只是一个相对的,无论您的css文件是否找到取决于您从(工作目录,这是相对的
例如,如果您从
src
中使用go run server / server.go
它会工作。如果你从src / server
文件夹中使用运行server.go
,它将无法工作。此外,如果您从您的应用程序创建一个本地可执行文件放入bin
文件夹中,并从bin
文件夹,这也不会工作,因为css
文件夹不在bin
文件夹中。
使用
从
或将src
运行server / server.gocss
文件夹复制到bin
文件夹并从bin启动可执行文件
文件夹,它应该工作(但在这种情况下,你还必须复制其他静态文件,如html模板)。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 containscss/somefilename.css
,src
also containsserver/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 foldersrc/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).这篇关于404页面未找到 - 转换渲染css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!