Javascript 从Go web服务器查找HTML上的资产时遇到问题

Javascript 从Go web服务器查找HTML上的资产时遇到问题,javascript,html,go,server,filesystems,Javascript,Html,Go,Server,Filesystems,我正在编辑我在网上找到的一些教程代码,并想添加一个前端。我让我的路由器吐出我的html没有问题,但html找不到我的静态文件。 这是我的主要功能 func main() { router := NewRouter() cssHandler := http.FileServer(http.Dir("./css/")) imagesHandler := http.FileServer(http.Dir("./images/")) scriptHandler := http.F

我正在编辑我在网上找到的一些教程代码,并想添加一个前端。我让我的路由器吐出我的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>

去做吧
{{todo}}

如果你想看到所有的代码,这里是一个回购我的工作 我试着把东西放在一个分组的静态文件夹中,最近我把scripts文件夹移到了基本目录中。如果你有任何好的教程链接,请让我知道,到目前为止我发现的一切都没有帮助。
谢谢

终于让它工作了,你必须把./放在前面,否则它就不工作了

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中/ 即../static/scripts/app.js
TLDR将每个资产路由视为路由器上的路由,而不是文件系统上的空间

我收到错误:./main.go:9:undefined:NewRouter您可以修复并推送代码以便我检查吗!我看不到你的css和图像文件夹?@PravinMishra try go run*.go我还没有css或图像,只是脚本,我想如果我能让脚本工作,我就能让其余的工作。请看一下这段代码:@PravinMishra try你的代码更新成这样| func服务器资源(w http.ResponseWriter,req*http.Request){print(“service”)path:=req.URL.path print(req.URL.path)http.ServeFile(w,req,path)}func main(){router:=NewRouter()http.HandleFunc(“/scripts/app.js”,serversource)http.HandleFunc(/scripts/todoCtrl.js”,serversource)log.Fatal(http.ListenAndServe(“:8080”,router))}然而,我从来没有更好地输入过serve FUNCTION:不要使用相对路径。让你的应用程序使用绝对路径,不管它们是通过环境变量还是配置文件传递的。@elithrar好主意,你能给我举个例子吗?我对go的后端还是很陌生的。