Golang Web 服务器不提供静态文件
Golang web server not serving static files
我很确定我忽略了一些明显的东西,但我不确定是什么。我正在构建一个简单的 Web 应用程序,它提供书籍的模板页面。模板工作正常,图像路径似乎正确填充,但我不断收到图像本身的 404 错误。
这是模板:
<h1>{{.Title}}</h1>
<h2>{{.Author.Name}}</h2>
<image src="../images/{{.ImageURI}}" />
这是应用程序本身:
package main
import (
"html/template"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/user/marketplace/typelibrary"
)
var books []typelibrary.Book
func ItemHandler(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var selected typelibrary.Book
//Retrieve item data
for _, item := range books {
if item.ID == params["id"] {
selected = item
break
}
}
t, _ := template.ParseFiles("./templates/book.html")
t.Execute(w, selected)
}
func main() {
router := mux.NewRouter()
books = append(books, typelibrary.Book{ID: "1", Title: "The Fellowship of the Ring", ImageURI: "LotR-FotR.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}})
books = append(books, typelibrary.Book{ID: "2", Title: "The Two Towers", ImageURI: "LotR-tTT.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}})
books = append(books, typelibrary.Book{ID: "3", Title: "The Return of the King", ImageURI: "LotR-RotK.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}})
books = append(books, typelibrary.Book{ID: "4", Title: "Monster Hunter International", ImageURI: "MHI1.jpg", Author: &typelibrary.Author{Name: "Larry Correia"}})
router.Handle("/", http.FileServer(http.Dir(".")))
router.Handle("/images/", http.FileServer(http.Dir("../images/")))
router.HandleFunc("/item/{id}", ItemHandler).Methods("GET")
srv := &http.Server{
Handler: router,
Addr: ":8080",
WriteTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
图像存储在 images
子目录中,就在可执行文件所在目录的正下方。当我尝试在页面中查看损坏的图像时,路径显示为 localhost:8080/images/[imagename]
但出现 404 错误。我在这里缺少哪些配置或路由选项?
您正在错误地创建路由来提供图像。 Router.Handle()
方法用 Path()
匹配器匹配 URL,它匹配整个路径,而你实际上想要匹配任何以“/image/”开头的路径。相反,使用 PathPrefix()
匹配器创建路由:
var imgServer = http.FileServer(http.Dir("./images/"))
router.PathPrefix("/images/").Handler(http.StripPrefix("/images/", imgServer))
有关详细信息,请参阅 https://godoc.org/github.com/gorilla/mux#Router.Handle。
我很确定我忽略了一些明显的东西,但我不确定是什么。我正在构建一个简单的 Web 应用程序,它提供书籍的模板页面。模板工作正常,图像路径似乎正确填充,但我不断收到图像本身的 404 错误。
这是模板:
<h1>{{.Title}}</h1>
<h2>{{.Author.Name}}</h2>
<image src="../images/{{.ImageURI}}" />
这是应用程序本身:
package main
import (
"html/template"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/user/marketplace/typelibrary"
)
var books []typelibrary.Book
func ItemHandler(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var selected typelibrary.Book
//Retrieve item data
for _, item := range books {
if item.ID == params["id"] {
selected = item
break
}
}
t, _ := template.ParseFiles("./templates/book.html")
t.Execute(w, selected)
}
func main() {
router := mux.NewRouter()
books = append(books, typelibrary.Book{ID: "1", Title: "The Fellowship of the Ring", ImageURI: "LotR-FotR.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}})
books = append(books, typelibrary.Book{ID: "2", Title: "The Two Towers", ImageURI: "LotR-tTT.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}})
books = append(books, typelibrary.Book{ID: "3", Title: "The Return of the King", ImageURI: "LotR-RotK.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}})
books = append(books, typelibrary.Book{ID: "4", Title: "Monster Hunter International", ImageURI: "MHI1.jpg", Author: &typelibrary.Author{Name: "Larry Correia"}})
router.Handle("/", http.FileServer(http.Dir(".")))
router.Handle("/images/", http.FileServer(http.Dir("../images/")))
router.HandleFunc("/item/{id}", ItemHandler).Methods("GET")
srv := &http.Server{
Handler: router,
Addr: ":8080",
WriteTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
图像存储在 images
子目录中,就在可执行文件所在目录的正下方。当我尝试在页面中查看损坏的图像时,路径显示为 localhost:8080/images/[imagename]
但出现 404 错误。我在这里缺少哪些配置或路由选项?
您正在错误地创建路由来提供图像。 Router.Handle()
方法用 Path()
匹配器匹配 URL,它匹配整个路径,而你实际上想要匹配任何以“/image/”开头的路径。相反,使用 PathPrefix()
匹配器创建路由:
var imgServer = http.FileServer(http.Dir("./images/"))
router.PathPrefix("/images/").Handler(http.StripPrefix("/images/", imgServer))
有关详细信息,请参阅 https://godoc.org/github.com/gorilla/mux#Router.Handle。