英文:
How can I find CSS files using golang Gorilla mux
问题
我正在使用Go语言和Gorilla Mux。
这是我的webserver.go文件:
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func HomeHandler(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "index.html")
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
http.Handle("/", r)
log.Println("Server running on :8080")
err := http.ListenAndServe(":8080", r)
if err != nil {
log.Printf("Error: %s\n", err.Error())
}
}
在与webserver.go文件位于同一文件夹的位置上,有一个index.html文件。
/ - 这里是index.html
/css - 所有的CSS文件
/images - 所有的图片和资源文件
我成功使用上述代码加载了index.html文件,但似乎无法加载CSS文件和图片。
在index.html文件中,我有以下内容:
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
所以它应该能够找到CSS文件,或者我需要确保"Go"能够找到css和image文件夹吗?如何做到这一点?
英文:
I'm using Go with Gorilla Mux.
This is my webserver.go file
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func HomeHandler(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "index.html")
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
http.Handle("/", r)
log.Println("Server running on :8080")
err := http.ListenAndServe(":8080", r)
if err != nil {
log.Printf("Error: %s\n", err.Error())
}
}
In the same folder where the webserver.go file is located is the index.html file.
/ - here is the index.html
/css - All the CSS files
/images - All the images, resource files
I manage to load the index.html file using the above code but it doesn't seem to load the CSS files and images.
inside the index.html file I have.
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
So it should find the css files or do I have to make sure that "Go" can find the css and image folder?
How?
答案1
得分: 5
你可以使用http.FileServer来独立地提供所有静态文件,而不依赖于gorilla/mux
。
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func HomeHandler(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "index.html")
}
func main() {
r := mux.NewRouter()
cssHandler := http.FileServer(http.Dir("./css/"))
imagesHandler := http.FileServer(http.Dir("./images/"))
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
http.Handle("/images/", http.StripPrefix("/images/", imagesHandler))
r.HandleFunc("/", HomeHandler)
http.Handle("/", r)
log.Println("Server running on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
英文:
You can use http.FileServer for serving all the static files independently of gorilla/mux
.
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func HomeHandler(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "index.html")
}
func main() {
r := mux.NewRouter()
cssHandler := http.FileServer(http.Dir("./css/"))
imagesHandler := http.FileServer(http.Dir("./images/"))
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
http.Handle("/images/", http.StripPrefix("/images/", imagesHandler))
r.HandleFunc("/", HomeHandler)
http.Handle("/", r)
log.Println("Server running on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论