dongpo2002 2017-06-20 20:30
浏览 53
已采纳

为什么我的golang模板没有选择外部javascript文件?

I tried so many suggestions from stack overflow but none seems to work. I was not able to pick the external js file.

My main function:

package main

import(
    "encoding/json"
    "net/http"
    "fmt"
    "github.com/gorilla/mux"
    "github.com/rs/cors"
    "text/template"
)  

func GetPeopleEndpoint(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/html")
    t, _ := template.ParseFiles("index2.html")
    t.Execute(w, nil)
}

func main() {
    router := mux.NewRouter()
    people = append(people, Person{ID: "1", Firstname: "Nic", Lastname: "Raboy", Address: &Address{City: "Dublin", State: "CA"}})
    people = append(people, Person{ID: "2", Firstname: "Maria", Lastname: "Raboy"})

    fmt.Println(people)

    router.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir("."))))
    router.HandleFunc("/people", GetPeopleEndpoint)

    c := cors.New(cors.Options{
        AllowedOrigins: []string{"http://localhost:3000"},
        AllowCredentials: true,
    })

    // Insert the middleware
    handler := c.Handler(router)

    //  handler := cors.Default().Handler(router)
    http.ListenAndServe(":12345", handler)
}

All my files are in the same directory.

Here is my html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    This is page2

    <div id='newdiv'>

    </div>
  </body>

  <script type="text/javascript" src="app2.js">
  </script>
</html>

The error I'm getting is "GET http://localhost:12345/app2.js". I don't know where I'm doing the mistake.

  • 写回答

3条回答 默认 最新

  • dongwei5740 2017-06-20 20:42
    关注

    This line:

    router.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir("."))))
    

    Means that requests coming in for URLs under "/files/" will be served from files on disk in the current directory, with the "/files/" prefix removed. So if you want a file in the current directory named app2.js, then the URL must be /files/app2.js.

    router.Handle defines what handler will handle a given path. In this case, that path is /files/. Because of the trailing slash, the same handler will be used for all URLs beginning with /files/.

    http.StripPrefix is a handler wrapper. It takes the incoming request, strips off the given prefix (in this case, /files/), removes it from the URL path, and then passes the request on to the handler passed to StripPrefix.

    http.FileServer serves files out of a http.FileSystem, in this case provided by http.Dir. http.Dir exposes the files in a directory, in this case the current working directory (".").

    So, in total: requests beginning with /files/, will have the /files/ part removed, then whatever is left, that file path will be looked for in the current working directory, and if it is found, it will be served. So, /files/app2.js will serve ./app2.js. Your HTML must reference /files/app2.js, not app.js.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目