嵌入式文件系统如何正确提供使用 html/template 生成的内容
来源:stackoverflow
时间:2024-02-10 14:45:23 105浏览 收藏
目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《嵌入式文件系统如何正确提供使用 html/template 生成的内容》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~
当我在浏览器上访问 localhost:8080
时,我得到一个显示模板中正确字段值的页面,但是,css 无法正确加载。当我访问浏览器上的 web 开发人员工具面板时,我注意到 .js
和 .css
文件有两个相同的错误,即
the resource from “http://localhost:8080/static/style.css” was blocked due to mime type (“text/plain”) mismatch (x-content-type-options: nosniff).
(我只显示与 .css
文件相关的错误,其他相同)
我真的不知道还能做什么,因为我已经尝试了所有简单的解决方案,例如修改相对路径或在与 main.go
相同的级别构建二进制文件并从那里运行服务器。 p>
据我所知,嵌入对 templates
文件夹有效,但似乎对 static
文件夹的内容不起作用。我认为我没有错说,虽然服务器成功加载模板,但在解析 dom 时无法找到 /static/styles.css
(对于 .js
文件也是如此)(它找不到/静态/文件夹)。
在这一点上,任何人的猜测都和我的一样好。
我做错了什么?为什么服务器无法成功路由 \static\
文件夹?我的代码还有其他问题吗?
老实说,我现在看到的是胡言乱语。欢迎任何帮助。谢谢
我的代码
我的文件夹结构如下
static | script.js styles.css templates | template.html handler.go main.go
这是我的 main.go
:
package main import ( "embed" "fmt" "net/http" ) //go:embed static templates var content embed.fs func main() { // define the handler function for the page to display http.handlefunc("/", handler) // serve the static files (css, js, images) from the "static" directory http.handle("/static/", http.stripprefix("/static/", http.fileserver(http.fs(content)))) // start the server on port 8080 http.listenandserve(":8080", nil) }
这是我的 handler.go
文件:
package main import ( "html/template" "net/http" "time" /* other imports */ ) func handler(w http.responsewriter, r *http.request) { // define the template data data, err := /* code that returns data for the template */ if err != nil { panic(err) // i wasn't sure how to handle this } // parse the template file tmpl, err := template.parsefs(content, "templates/template.html") if err != nil { http.error(w, err.error(), http.statusinternalservererror) return } // execute the template with the data err = tmpl.execute(w, data) if err != nil { http.error(w, err.error(), http.statusinternalservererror) return } }
这是我的 template.html
文件:
Countdown Title
/* here is code accessing the fields of the data passed to the template */
我不提供 script.js
文件,因为它暂时是空的,而且它引起的问题与 style.css
的问题相同。
正确答案
static
不是文件系统的根目录,因此 http.stripprefix
不是必需的。
使用它,对 /static/some-file.js
的请求会在文件系统中查找 /some-file.js
。如果没有它,/static/
会被保留,这就是文件系统的实际布局方式。
所以,你想要:
// Serve the static files (CSS, JS, images) from the "static" directory http.Handle("/static/", http.FileServer(http.FS(content)))
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习