dqnhfbc3738 2016-07-10 02:57
浏览 26
已采纳

无法通过Go Web服务器在我的HTML上找到资产

I am editing some tutorial code I found online, and wanted to add a front end. I got my router spitting out my html no problem, but the html cannot find my static files. here is my main function

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))

}

here is my index

<!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>

if you want to see all the code, here is a repo i am working on https://github.com/kekeoki/go-do-it I have tried putting things in a grouped static folder, and most recently I moved the scripts folder to the base directory. If you have any good tutorial links, please let me know, everything I have found so far has not helped. Thanks

  • 写回答

1条回答 默认 最新

  • doutuo6689 2016-07-10 07:23
    关注

    Finally got it working, YOU MUST HAVE THE ./ IN FRONT OR IT WONT WORK.

    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))
    }
    

    my scripts are in ./static/scripts/ I.E. ./static/scripts/app.js TLDR treat each asset route as a route on your router, not a space on your file system

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建