go - 如何在 golang 中提供静态文件
问题描述
我正在尝试在 golang 中创建一个简单的网络服务器,但我不知道如何提供静态 css 文件。mi项目的结构如下:
project folder
->static
->templates
->index.gohtml
->styles
->style.css
在我的模板中,我有这个简单的 html 行:<link href="/styles/styles.css" type="text/css">
在 main.go 我有这个:
http.Handle("/styles", http.FileServer(http.Dir("./static/styles")))
...
//show the backend homepage that refers to index.gohtml
http.HandleFunc("/backend/home", handler)
解决方案
现在当他们点击时/styles
,它会尝试访问./static/styles/styles
,所以你通常像这样去掉前缀:
http.Handle("/styles/", http.StripPrefix("/styles/", http.FileServer(http.Dir("./static/styles"))))
推荐阅读
- c++ - 最小堆比较器
- python - 带有 EndUser 模型的任务模型
- unity3d - 为什么矢量和 OnMove 函数采用不同的轴?
- typescript - STRIPE + TypeScript:TypeError:stripe_1.default 不是构造函数
- c++ - 使用鼠标单击和鼠标拖动来环绕立方体(Opengl)
- c++ - 以指针为键复制 std::map
- freemarker - 使用 apache freemarker 进行字符串拆分和验证
- javascript - 如何使用 JQuery 根据 ID 查找 Text 值?
- php - 为什么 PHP 会话在某些浏览器中不起作用?
- django - AWS RDS 主密码每 2 天重置一次