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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路