在我的 HTML 上查找资产时遇到问题,从 Go 网络服务器提供服务
Trouble finding assets on my HTML, serving from a Go web server
我正在编辑网上找到的一些教程代码,想添加一个前端。我的路由器吐出我的 html 没问题,但是 html 找不到我的静态文件。
这是我的主要功能
func main() {
router := NewRouter()
cssHandler := http.FileServer(http.Dir("./css/"))
imagesHandler := http.FileServer(http.Dir("./images/"))
scriptHandler := http.FileServer(http.Dir("./scripts/"))
http.Handle("/scripts/", http.StripPrefix("/scripts/", scriptHandler))
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
http.Handle("/images/", http.StripPrefix("/images/", imagesHandler))
log.Fatal(http.ListenAndServe(":8080", router))
}
这是我的索引
<!DOCTYPE html>
<html>
<head>
<title>Go Do IT</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script type="text/javascript" src="./scripts/app.js"></script>
<script type="text/javascript" src="./scripts/toDoCtrl.js"></script>
</head>
<body ng-app="app">
<div ng-controller="toDoCtrl as ctrl">
<div ng-repeat ="todo in ctrl.todos">
{{todo}}
</div>
</div>
</body>
如果您想查看所有代码,这是我正在处理的一个回购 https://github.com/kekeoki/go-do-it
我试过将东西放在一个分组的静态文件夹中,最近我将脚本文件夹移动到基本目录。如果您有任何好的教程链接,请告诉我,到目前为止我发现的所有内容都没有帮助。
谢谢
终于让它工作了,你必须在前面有 ./ 否则它不会工作。
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func ServeStatic(router *mux.Router, staticDirectory string) {
staticPaths := map[string]string{
"styles": staticDirectory + "/styles/",
"bower_components": staticDirectory + "/bower_components/",
"images": staticDirectory + "/images/",
"scripts": staticDirectory + "/scripts/",
}
fmt.Println(staticPaths)
for pathName, pathValue := range staticPaths {
pathPrefix := "/" + pathName + "/"
router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
http.FileServer(http.Dir(pathValue))))
fmt.Println(pathValue)
}
}
func main() {
router := NewRouter()
staticDirectory := "./static"
ServeStatic(router, staticDirectory)
log.Fatal(http.ListenAndServe(":8080", router))
}
我的脚本在./static/scripts/
IE。 ./static/scripts/app.js
TLDR 将每个资产路由视为路由器上的路由,而不是文件系统上的 space
我正在编辑网上找到的一些教程代码,想添加一个前端。我的路由器吐出我的 html 没问题,但是 html 找不到我的静态文件。 这是我的主要功能
func main() {
router := NewRouter()
cssHandler := http.FileServer(http.Dir("./css/"))
imagesHandler := http.FileServer(http.Dir("./images/"))
scriptHandler := http.FileServer(http.Dir("./scripts/"))
http.Handle("/scripts/", http.StripPrefix("/scripts/", scriptHandler))
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
http.Handle("/images/", http.StripPrefix("/images/", imagesHandler))
log.Fatal(http.ListenAndServe(":8080", router))
}
这是我的索引
<!DOCTYPE html>
<html>
<head>
<title>Go Do IT</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script type="text/javascript" src="./scripts/app.js"></script>
<script type="text/javascript" src="./scripts/toDoCtrl.js"></script>
</head>
<body ng-app="app">
<div ng-controller="toDoCtrl as ctrl">
<div ng-repeat ="todo in ctrl.todos">
{{todo}}
</div>
</div>
</body>
如果您想查看所有代码,这是我正在处理的一个回购 https://github.com/kekeoki/go-do-it 我试过将东西放在一个分组的静态文件夹中,最近我将脚本文件夹移动到基本目录。如果您有任何好的教程链接,请告诉我,到目前为止我发现的所有内容都没有帮助。 谢谢
终于让它工作了,你必须在前面有 ./ 否则它不会工作。
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func ServeStatic(router *mux.Router, staticDirectory string) {
staticPaths := map[string]string{
"styles": staticDirectory + "/styles/",
"bower_components": staticDirectory + "/bower_components/",
"images": staticDirectory + "/images/",
"scripts": staticDirectory + "/scripts/",
}
fmt.Println(staticPaths)
for pathName, pathValue := range staticPaths {
pathPrefix := "/" + pathName + "/"
router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
http.FileServer(http.Dir(pathValue))))
fmt.Println(pathValue)
}
}
func main() {
router := NewRouter()
staticDirectory := "./static"
ServeStatic(router, staticDirectory)
log.Fatal(http.ListenAndServe(":8080", router))
}
我的脚本在./static/scripts/ IE。 ./static/scripts/app.js TLDR 将每个资产路由视为路由器上的路由,而不是文件系统上的 space