404页面未找到 - 转换渲染css文件 [英] 404 page not found - Go rendering css file

查看:373
本文介绍了404页面未找到 - 转换渲染css文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在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.go 或将 css 文件夹复制到 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 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 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).

这篇关于404页面未找到 - 转换渲染css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆