如何在Gin中提供静态文件

时间:2019-01-29 04:52:33

标签: go gin

通过遵循本教程,我尝试将前端(React)连接到后端api(Gin),但是static.Serve无法正常工作,出现以下错误提示:

cannot use static.Serve("/", static.LocalFile("./views", true)) (type "github.com/gin-gonic/gin".HandlerFunc) as type "github.com/supebirdgz/amgmt/vendor/github.com/gin-gonic/gin".HandlerFunc in argument to router.Use

来源:

import (
    "github.com/gin-gonic/gin"
    "github.com/gin-gonic/contrib/static"
)

func main()  {
    router := gin.Default()
    router.Use(static.Serve("/", static.LocalFile("./frontend", true)))
    router.Run()
}

如果Gin中有更新?我试图用其他人替换静态包,还是一样。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您忘记加载

之类的文件路径
  r := gin.Default()
  r.LoadHTMLGlob("dist/*.html")    // load the built dist path
  r.LoadHTMLFiles("static/*/*") //  load the static path
  r.Static("/static", "./dist/static")  // use the loaded source
  r.StaticFile("/hello/", "dist/index.html")  // use the loaded source

  r.Run(":8080")