在外出的Web服务器上使用外部CSS文件 [英] Using external CSS file on a go web server

查看:319
本文介绍了在外出的Web服务器上使用外部CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现一个简单的wiki,如下所示=>
https:/ /golang.org/doc/articles/wiki/



我知道这已被问了好几次,但我无法获得静态内容加载在我的代码中。这很愚蠢,我按照指示添加了静态内容处理程序,但CSS仍然没有在html文件中使用。



我添加了这样的处理程序:

  http.Handle(tmp / css,http.StripPrefix(tmp / css,http.FileServer(http .dir(tmp / css))))
http.Handle(tmp / img,http.StripPrefix(tmp / img,http.FileServer(http.Dir(tmp / img)) )))

整个代码可以在这里看到,在我的github页面上=> https://github.com/Skarlso/goprojects/tree/master/golangwiki



感谢您的帮助!
Gergely。

解决方案

由于您使用相对路径(例如 http.Dir(tmp / css),重要的是从哪个文件夹启动你的应用程序。

请阅读: 404找不到页面 - 去渲染css文件为什么我需要使用http.StripPrefix来访问我的静态文件请注意,您的网页在 / edit / 下可用, code> / view / ,但HTML模板包含使用 relative url的CSS资源:

 < link rel =stylesheethref =css / styles.css> 

因此,例如,结果将会是 /view/css/styles.css - 不是您要的!


I'm trying to implement a simple wiki as depicted here => https://golang.org/doc/articles/wiki/

I know this has been asked several times, but I can't get the static content to load in my code. It's stupid, I followed the instructions which let's me add a handler for static content but the CSS still does not get used in the html file.

I added the handlers like this:

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

The whole code can be seen here, on my github page => https://github.com/Skarlso/goprojects/tree/master/golangwiki

Thanks for the help! Gergely.

解决方案

Since you use relative paths (e.g. http.Dir("tmp/css"), it is important how (from which folder) you start your app.

Please read: 404 page not found - Go rendering css file and Why do I need to use http.StripPrefix to access my static files? for more details.

Also note that your pages are available under /edit/ and /view/, but the HTML templates include CSS resources using relative urls:

<link rel="stylesheet" href="css/styles.css">

So e.g. the result will be /view/css/styles.css - not what you want!

这篇关于在外出的Web服务器上使用外部CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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