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

http handler 绑定相同地址,求助

  •  
  •   zgoing · Oct 26, 2017 via iPhone · 2055 views
    This topic created in 3303 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
    boboliu
    Oct 26, 2017 via Android❤️1
    1
    可以写个 handler,当 URL 为 /时返回 index,别的返回 FileServe

    其实最简单的还是把 index 放在目标目录吧(手动滑稽
    zgoing
    zgoing
    OP
    Oct 26, 2017 via iPhone
    2
    @boboliu @boboliu 非常感谢
    freestyle
    freestyle
    Oct 29, 2017❤️1
    3
    @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
    zgoing
    OP
    Oct 30, 2017
    4
    @freestyle #3 谢谢说明,学习了
    boboliu
    boboliu
    Oct 30, 2017 via Android
    5
    @freestyle 感谢说明,在下还是不熟练啊_(:з」∠)_
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2293 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:28 · PVG 21:28 · LAX 05:28 · JFK 08:28
    ♥ Do have faith in what you're doing.