1

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

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

and index.html has <link rel="modulepreload" href="/_app/immutable/pages/index.svelte-6d5b7005.js"/> 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 ?

sandeep.kgp
  • 847
  • 2
  • 10
  • Examples for serving static files are in gin's readme on github. More examples are in gin-gonic/examples github project. It is customary that you first do some research before asking a question. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – mkopriva Jun 20 '22 at 16:34
  • @mkopriva serving static index.html is working fine but unable to find any resource of loading resources that are accessed inside index.html like css or js – sandeep.kgp Jun 20 '22 at 16:44
  • `index.html` is a file not a directory. It doesn't contain any files for you to access, only references that can be used by the browser to subsequently request additional files and those you must serve, but you only serve one file. Examples of how to serve files from a directory are, as already said, to be found in the readme. – mkopriva Jun 20 '22 at 16:46

1 Answers1

2

You have to serve whole directory

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

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

kuklyy
  • 316
  • 3
  • 13