如何在Go服务器中提供嵌套的静态文件?

huangapple go评论68阅读模式
英文:

How to serve nested static files in go server?

问题

我无法使用gin服务器提供网站服务。这是Go代码片段:

router := gin.Default()
router.StaticFile("/", "./build/index.html")

index.html文件中有<link rel="modulepreload" href="/_app/immutable/pages/index.svelte-6d5b7005.js"/>以及一些CSS链接。

现在,服务器可以正常提供index.html文件,但是js模块没有被加载

我该如何提供index.html文件以便所有嵌套的文件/模块也能被提供?

英文:

I am unable to serve a website using gin server. Here's the go code snippet:

router := gin.Default()
router.StaticFile(&quot;/&quot;, &quot;./build/index.html&quot;)

and index.html has &lt;link rel=&quot;modulepreload&quot; href=&quot;/_app/immutable/pages/index.svelte-6d5b7005.js&quot;/&gt; and some css links too.

Now, the server serves index.html file fine but the js modules aren't being loaded.

How do I serve the index.html in a way that all the nested files/modules also get served
?

答案1

得分: 2

你必须提供整个目录

router.Static("/", "./build")

但是请再次确认是否所有来自build目录的文件都应该公开可用。

英文:

You have to serve whole directory

router.Static(&quot;/&quot;, &quot;./build&quot;)

But double check if all files from build dir should be available publicly.

huangapple
  • 本文由 发表于 2022年6月20日 23:52:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/72689771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定