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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器