The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
zgoing

http handler 绑定相同地址,求助

  •  
  •   zgoing · Oct 26, 2017 via iPhone · 1657 views
    This topic created in 3132 days ago, the information mentioned may be changed or developed.
    希望在"/"下注册 FileServer,同时也想在浏览器访问 /时发送 index.html 文件,请问有什么办法可以实现呢。google 一下只能找到 15 年 github 上的一个 issue,最后也还是没有解决
    5 replies    2017-10-30 06:42:12 +08:00
    boboliu
        1
    boboliu  
       Oct 26, 2017 via Android   ❤️ 1
    可以写个 handler,当 URL 为 /时返回 index,别的返回 FileServe

    其实最简单的还是把 index 放在目标目录吧(手动滑稽
    zgoing
        2
    zgoing  
    OP
       Oct 26, 2017 via iPhone
    @boboliu @boboliu 非常感谢
    freestyle
        3
    freestyle  
       Oct 29, 2017   ❤️ 1
    @zgoing
    @boboliu
    http.FileServer 会自动处理 index.html 的, 你看代码 https://github.com/golang/go/blob/master/src/net/http/fs.go#L592-L605

    比如 http.Handle("/", http.FileServer( http.Dir("assets"))), 在 assets 文件夹下如果有 index.html, 就会显示出来


    package main

    import (
    "net/http"
    "log"
    )

    func main() {
    http.Handle("/", http.FileServer( http.Dir("assets")))
    log.Print("http file server start at :8000")
    log.Fatal( http.ListenAndServe(":8000", nil))
    }
    zgoing
        4
    zgoing  
    OP
       Oct 30, 2017
    @freestyle #3 谢谢说明,学习了
    boboliu
        5
    boboliu  
       Oct 30, 2017 via Android
    @freestyle 感谢说明,在下还是不熟练啊_(:з」∠)_
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3111 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 13:53 · PVG 21:53 · LAX 06:53 · JFK 09:53
    ♥ Do have faith in what you're doing.