英文:
returned 404 error when load js file
问题
当我尝试渲染包含一个指向js文件的脚本链接的html文件时,出现了错误。但是当我加载页面时,出现了以下错误:
Started GET "/views/script.js" .... Returning 404
我的文件夹结构如下:
|--todolist
|--main.go
|--views/
|--index.html
|--script.js
main.go
package main
import (
"github.com/zenazn/goji"
"html/template"
"net/http"
)
func renderHTMLPage(w http.ResponseWriter, path string) {
t, err := template.ParseFiles(path)
if err != nil {
panic(err)
}
t.Execute(w, nil)
}
func Index(w http.ResponseWriter, r *http.Request) {
renderHTMLPage(w, "./views/index.html")
}
func main() {
goji.Get("/", Index)
goji.Serve()
}
views/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Le titre du document</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="script.js"></script>
<h1>To-Do List </h1>
<ul id="todolist">
<li> Hello <button>Delete</button></li>
<li> Wesh <button>Delete</button></li>
</ul>
<input type="text" id="new-text" /><button id="add">Add</button>
</body>
</html>
views/script.js
function addListItem() {
var text = $('#new-text').val()
if (text != "") {
$('#todolist').append('<li>'+text+'<button id="dede" name=\"' + i + '\">Delete</button></li>')
}
$('#new-text').val("")
}
function deleteItem() {
$(this).parent().remove()
}
$(function() {
$('#add').on('click', addListItem);
$("#todolist").on("click", "#dede", deleteItem)
});
我该如何使它正确加载js文件?
创建一个仅使用jQuery/JavaScript和Golang API的应用程序的最佳方法是什么?
谢谢。
英文:
I get an error when i would like to render html file which contains a script link on js file.
But when i load page i get this error :
Started GET "/views/script.js" .... Returning 404
my folder is like this
|--todolist
|--main.go
|--views/
|--index.html
|--script.js
main.go
package main
import (
"github.com/zenazn/goji"
"html/template"
"net/http"
)
func renderHTMLPage(w http.ResponseWriter, path string) {
t, err := template.ParseFiles(path)
if err != nil {
panic(err)
}
t.Execute(w, nil)
}
func Index(w http.ResponseWriter, r *http.Request) {
renderHTMLPage(w, "./views/index.html")
}
func main() {
goji.Get("/", Index)
goji.Serve()
}
views/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Le titre du document</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="script.js"></script>
<h1>To-Do List </h1>
<ul id="todolist">
<li> Hello <button>Delete</button></li>
<li> Wesh <button>Delete</button></li>
</ul>
<input type="text" id="new-text" /><button id="add">Add</button>
</body>
</html>
views/script.js
function addListItem() {
var text = $('#new-text').val()
if (text != "") {
$('#todolist').append('<li>'+text+'<button id="dede" name=\"' + i + '\">Delete</button></li>')
}
$('#new-text').val("")
}
function deleteItem() {
$(this).parent().remove()
}
$(function() {
$('#add').on('click', addListItem);
$("#todolist").on("click", "#dede", deleteItem)
});
How can I do to make it properly load the js file ?
And what is the best way to create an app who use only jquery/javascript with a golang api on architecture ?
Thank you
答案1
得分: 1
你需要做以下操作之一:
- 从包含
/views
目录的路径提供服务 - 例如goji.Get("/views/*", http.FileServer(http.Dir("/Users/matt/Desktop/views/")))
- 使用
http.StripPrefix
(推荐)
下面的代码允许你将路径与目录名称解耦:
func main() {
goji.Get("/views/*", http.StripPrefix("/views", http.FileServer(http.Dir("/Users/matt/Desktop/views/"))))
goji.Get("/", Index)
goji.Serve()
}
我建议不要从根目录/*
提供服务,最好从专用的资源路径提供服务,这样在缓存、与CDN交互等方面更容易处理。
英文:
You need to either:
- Serve from the directory containing
/views
- e.g. goji.Get("/views/*", http.FileServer(http.Dir("/Users/matt/Desktop/views/")))` - Use
http.StripPrefix
(recommended)
The below allows you to decouple the path from the directory name:
func main() {
goji.Get("/views/*", http.StripPrefix("/views", http.FileServer(http.Dir("/Users/matt/Desktop/views/"))))
goji.Get("/", Index)
goji.Serve()
}
I'd recommend against serving from the 'root' - /*
. Better to serve from a dedicated assets path as it makes it easier when looking at caching, interacting with CDNs, etc.
答案2
得分: 0
在这种特殊情况下,代码应该如下所示:
goji.Get("/", Index)
goji.Get("/*", http.FileServer(http.Dir("./views")))
goji.Serve()
但是我必须说,将模板文件和静态文件保存在同一个目录中并从根目录提供静态文件是一个不好的主意。
英文:
In this particular case code should look like
goji.Get("/", Index)
goji.Get("/*", http.FileServer(http.Dir("./views")))
goji.Serve()
But I must say keeping template and static files in same directory is bad idea as well as serving static files from root.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论