admin 管理员组文章数量: 1086019
I bundle React app with Webpack after I call it in HTML file. However, it error when I using Golang and html/template to view HTML file.
My HTML file: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Note App</title>
</head>
<body>
<div id="root"></div>
<script src="public/bundle.js"></script>
</body>
</html>
My Golang file: index.go
package main
import (
"net/http"
"html/template"
"path/filepath"
)
func handle(w http.ResponseWriter, r *http.Request) {
fp := filepath.Join("views", "index.html")
t := template.Must(template.ParseFiles(fp))
t.Execute(w, nil)
}
func main() {
http.HandleFunc("/", handle)
http.ListenAndServe(":8080", nil)
}
I bundle React app with Webpack after I call it in HTML file. However, it error when I using Golang and html/template to view HTML file.
My HTML file: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Note App</title>
</head>
<body>
<div id="root"></div>
<script src="public/bundle.js"></script>
</body>
</html>
My Golang file: index.go
package main
import (
"net/http"
"html/template"
"path/filepath"
)
func handle(w http.ResponseWriter, r *http.Request) {
fp := filepath.Join("views", "index.html")
t := template.Must(template.ParseFiles(fp))
t.Execute(w, nil)
}
func main() {
http.HandleFunc("/", handle)
http.ListenAndServe(":8080", nil)
}
Share
Improve this question
asked Jul 3, 2018 at 3:07
Phuc HoangPhuc Hoang
551 silver badge4 bronze badges
2
- What is the error? – Jonathan Hall Commented Jul 3, 2018 at 7:58
- @V5NXT Please don't use snippets for Go language code; they only work for HTML, CSS, and JavaScript. – Heretic Monkey Commented Jul 3, 2018 at 12:51
2 Answers
Reset to default 8The issue seems to be that you are spinning up a server that only serves the HTML template - not the script. When a browser attempts to load your scripts, the server returns your index page.
Take a look at https://www.alexedwards/blog/serving-static-sites-with-go; this post discusses how you can serve static files.
For your purposes, you may be able to get by just by adding the following lines to the beginning of your main method:
fs := http.FileServer(http.Dir("public"))
http.Handle("/public/", http.StripPrefix("/static/", fs))
This will load any files in your directory, "public" (relative to your piled executable), and serve them at url/public/path/to/file
As a word of caution: by default, this will enable directory listings for your public directory (users will be able to see a list of files in that directory and all subdirectories). Take a look at the answer for this question for info on how to disable directory listings: https://stackoverflow./a/40718195/6346483
Are you serving public/bundle.js
?
If not you need to serve it somehow. For example you can use net/http
https://golang/pkg/net/http/#ServeFile
本文标签: goGolang html template cannot call javascript file in html fileStack Overflow
版权声明:本文标题:go - Golang html template cannot call javascript file in html file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744081201a2530278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论