dongmeba4877 2016-03-25 22:24
浏览 147
已采纳

重定向对静态文件的请求

I'm trying to serve a static html file, and this file has script tags that point to other resources. I want to serve the html file from one directory but then redirect requests for assets to another directory. This is how I'm setting it up now:

// server.go
import (
    "fmt"
    "html/template"
    "log"
    "net/http"
    "path"
    "time"
)

func handle(w http.ResponseWriter, r *http.Request) {
    lp := path.Join("./", "index.html")
    fmt.Println(lp)
    tmpl, err := template.ParseFiles(lp)
    if err != nil {
        log.Fatal(err)
    }
    tmpl.ExecuteTemplate(w, "index", nil)
}

func main() {
    fs := http.FileServer(http.Dir("../../app_assets/"))
    http.Handle("/assets", fs)
    http.HandleFunc("/static/", handle)

    fmt.Println("Go Server listening on port 8000")
    http.ListenAndServe(":8000", nil)
}

Here is my template:

<!-- index.html -->
{{define "index"}}
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="assets/css/libs.css" type="text/css" />
</head>

<body>
<script type="text/javascript" src="assets/js/libs.js"></script>

<h1> Hello </h1>
</body>
</html>
{{end}}

I'm able to serve the index file from localhost:8000/static/, but asset requests are not going to the assets folder two levels up (../../). What am I doing wrong?

NOTE:

I'm getting this error in the console when libs.js is loaded:

Uncaught SyntaxError: Unexpected token <

This leads me to believe that the request for the libs.js file is being redirected to the html markup.

How is this happening?

NOTE 2:

When I browse the result of the request for libs.js, I see the html markup. Even after using StripPrefix as advised below. What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dougengqiu8031 2016-03-26 04:47
    关注

    In your html when you specify the src attributes of your css and js you have src="assets/js/libs.js". This makes those files to be requested relative to the current path. So the request goes to http://localhost:8000/static/assets/js/libs.js.

    Since this has the /static prefix it will be handled by your /static handler and hence the html file is served.

    To make it go to /assets handler, specify the src with a / prefixed.

    <script type="text/javascript" src="/assets/js/libs.js"></script>
    

    Now the server will look for the file at ../../app_assets/assets/js/libs.js.

    If you want it to be ../../app_assets/js/libs.js, you can use StripPrefix to take out the assets part from the url in the server.

    fs := http.StripPrefix("/assets/", http.FileServer(http.Dir("../../app_assets/")))
    http.Handle("/assets/", fs)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改