如何在 Go 中加载 Angular 构建文件

我的 Go 项目中有一个 dist 文件夹。包含角度构建文件。


import (

    "github.com/gin-contrib/cors"

    "github.com/gin-gonic/contrib/static"

    "github.com/gin-gonic/gin"

)

func SetRoutes() *gin.Engine {

    router := gin.Default()

    router.Use(cors.Default())

    router.Use(static.Serve("/", static.LocalFile("./client/dist", true)))

    router.Run(":8080")

}

使用上面的代码我可以提供 dist 文件夹文件。但如果我刷新浏览器,它将无法获取这些文件。显示错误“404 页面未找到”。


神不在的星期二
浏览 75回答 2
2回答

qq_笑_17

router.Static("/", "./client/dist")试试这个代码。它应该有效。

收到一只叮咚

确保“dist”文件夹包含“index.html”文件作为直接子文件,因为“ng build”创建一个文件夹,其中“your-project-name”包含“index.html”并放置在“dist”中文件夹。
打开App,查看更多内容
随时随地看视频慕课网APP