加载模块脚本失败:预期为JavaScript模块。

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

Failed to load module script: Expected a JavaScript module

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我正在使用vite作为我的React应用程序的构建工具,后端使用Golang。

我已经为生产环境构建了应用程序,并将其托管在我的HTTP服务器上。

我的目录结构如下:

  1. server
  2. |- dist
  3. | | index.html
  4. | |- assets
  5. | | index.js
  6. | | index.css
  7. | main.go

为了托管我的文件,代码如下(在main.go中):

  1. fs := http.FileServer(http.Dir("./dist"))
  2. http.Handle("/", fs)

在index.html中:

  1. <script type="module" crossorigin src="/assets/index.fd457ca0.js"></script>
  2. <link rel="stylesheet" href="/assets/index.bdcfd918.css">

代码实际上发送了正确的文件,但是使用了错误的头部。

英文:

加载模块脚本失败:预期为JavaScript模块。

I am using vite as build tool for my react app and golang as backend.

I built the app for production and host the app on my http server.

my directory structure:

  1. server
  2. |- dist
  3. | | index.html
  4. | |- assets
  5. | | index.js
  6. | | index.css
  7. | main.go

To host my files the code looks like (inside main.go)

  1. fs := http.FileServer(http.Dir(&quot;./dist&quot;))
  2. http.Handle(&quot;/&quot;, fs)

in index.html

  1. &lt;script type=&quot;module&quot; crossorigin src=&quot;/assets/index.fd457ca0.js&quot;&gt;&lt;/script&gt;
  2. &lt;link rel=&quot;stylesheet&quot; href=&quot;/assets/index.bdcfd918.css&quot;&gt;

The code did actually send correct files but with wrong headers.

加载模块脚本失败:预期为JavaScript模块。

答案1

得分: 0

所以我不得不编写自己的文件服务器来手动设置头部,如下所示:

  1. contentTypeMap := map[string]string{
  2. ".html": "text/html",
  3. ".css": "text/css",
  4. ".js": "application/javascript",
  5. }
  6. filepath.Walk("./dist", func(path string, info os.FileInfo, err error) error {
  7. if err != nil {
  8. log.Fatalf(err.Error())
  9. }
  10. if info.IsDir() {
  11. return err
  12. }
  13. dirPath := filepath.ToSlash(filepath.Dir(path))
  14. contentType := contentTypeMap[filepath.Ext(info.Name())]
  15. handlePath := "/" + strings.Join(strings.Split(dirPath, "/")[1:], "/")
  16. hf := func(w http.ResponseWriter, r *http.Request) {
  17. w.Header().Add("Content-Type", contentType) // <---- 关键部分
  18. http.ServeFile(w, r, path)
  19. }
  20. if handlePath != "/" {
  21. handlePath += "/" + info.Name()
  22. }
  23. mainRouter.HandleFunc(handlePath, hf)
  24. return nil
  25. })

(如果代码有问题,请进行优化,我自己制作了这个解决方案,并尝试了很多方法来满足我的需求)

现在,我使用正确的头部接收到了正确的文件。

我找不到任何使用http包中的http.FileServer来处理自定义头部的解决方案。如果存在简单的解决方案,请提供。

英文:

So I had to write my own file server to set the headers manually like:

  1. contentTypeMap := map[string]string{
  2. &quot;.html&quot;: &quot;text/html&quot;,
  3. &quot;.css&quot;: &quot;text/css&quot;,
  4. &quot;.js&quot;: &quot;application/javascript&quot;,
  5. }
  6. filepath.Walk(&quot;./dist&quot;, func(path string, info os.FileInfo, err error) error {
  7. if err != nil {
  8. log.Fatalf(err.Error())
  9. }
  10. if info.IsDir() {
  11. return err
  12. }
  13. dirPath := filepath.ToSlash(filepath.Dir(path))
  14. contentType := contentTypeMap[filepath.Ext(info.Name())]
  15. handlePath := &quot;/&quot; + strings.Join(strings.Split(dirPath, &quot;/&quot;)[1:], &quot;/&quot;)
  16. hf := func(w http.ResponseWriter, r *http.Request) {
  17. w.Header().Add(&quot;Content-Type&quot;, contentType) // &lt;---- key part
  18. http.ServeFile(w, r, path)
  19. }
  20. if handlePath != &quot;/&quot; {
  21. handlePath += &quot;/&quot; + info.Name()
  22. }
  23. mainRouter.HandleFunc(handlePath, hf)
  24. return nil
  25. })

(please optimize if the code is bad, I made the solution myself and I tried so many stuff to fit my needs)

Now with that I recevied the correct files with correct headers.

And I couldn't find any solutions to work with custom headers using http.FileServer in http package. And please provide a easy solution if it exists.

huangapple
  • 本文由 发表于 2022年5月31日 12:01:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/72442139.html
匿名

发表评论

匿名网友

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

确定