hujun528
V2EX  ›  问与答

go gin 绑定静态文件 报错

  •  
  •   hujun528 · Mar 12, 2021 · 2293 views
    This topic created in 2067 days ago, the information mentioned may be changed or developed.
    func main(){

    r := gin.Default()
    r.POST("/api/CreateStrategy", createstrategy)
    r.GET("/api/FreightSpaceStrategy", getFreightSpaceStrategyData)

    //绑定静态文件夹
    r.Static("/", "./dist")

    }


    报错内容
    panic: wildcard segment '*filepath' conflicts with existing children in path '/*filepath'
    5 replies  •  2021-03-28 11:03:05 +08:00
    dreamans
    dreamans
    Mar 12, 2021
    1
    panic 的提示已经很明显了,假设你访问 /api/CreateStrategy 时程序无法判断你是访问./dist/api/CreateStrategy 这个文件路径还是 `createstrategy ` 这个 handler
    hujun528
    hujun528
    OP
    Mar 12, 2021
    2
    @dreamans 这个框架限制太死, “/" 作为整 个网站的静态目录 是很常见的 ,在 node.js 中,会优先去匹配 看是否有对应的静态文件,没有再去匹配 看是否有 绑定 对应的路由,在 gin 中 这样做竟然报错
    togou
    togou
    Mar 12, 2021
    3
    gin 这一点确实很恶心 路由搞个优先路由就行了 为了速度不值得
    maypu
    maypu
    Mar 27, 2021
    4
    遇到了同样的问题
    hujun528
    hujun528
    OP
    Mar 28, 2021
    5
    @maypu 我解决了,new 一个新路由,绑定静态资源就不会冲突
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   750 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:17 · PVG 07:17 · LAX 15:17 · JFK 18:17
    ♥ Do have faith in what you're doing.