首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Gin中提供静态文件

如何在Gin中提供静态文件
EN

Stack Overflow用户
提问于 2019-01-29 12:52:33
回答 1查看 5.4K关注 0票数 2

通过遵循教程,我尝试连接前端(React)到后端API (Gin),但static.Serve不工作,错误提示如下:

代码语言:javascript
复制
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

来源:

代码语言:javascript
复制
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中有什么更新了吗?我试着用其他包替换静态包,仍然是一样的。

EN

回答 1

Stack Overflow用户

发布于 2019-01-29 15:01:51

您忘记加载文件路径,如下所示

代码语言:javascript
复制
  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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54414043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档