【问题标题】:Golang and JavaScript modulesGolang 和 JavaScript 模块
【发布时间】:2020-01-26 04:56:51
【问题描述】:

我想将 Polymer LitElement 与 Go 后端一起使用。 使用 LitElement,我在 JavaScript 模块中实现了 Web 组件! 对于服务器端的路由,我使用这样的 Gorilla Mux

mux := mux.NewRouter()
mux.PathPrefix("/").Handler(http.FileServer(http.Dir("./wwwroot")))

这会正确加载静态 html 文件。当 html 文件引用实现 Web 组件的 js 文件时,我收到以下错误(在 Chrome 中):

加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。根据 HTML 规范对模块脚本强制执行严格的 MIME 类型检查。

当我将组件模块重命名为具有扩展名 mjs 时,文件正确加载,但 LitElement 的模块无法加载并出现相同的错误。由于我对所有第三方 JavaScript 模块的文件扩展名没有影响,我不知道如何解决这个问题。

(我想如果我使用 Polymer 3 而不是 LitElement,我会遇到同样的问题)

有什么想法吗?

更新

这是使用 curl 请求 lit-element.js JavaScript 模块的输出

PS C:\Test\Polymer\LitElement> curl http://localhost:8082/node_modules/lit-element/lit-element.js

StatusCode        : 200
StatusDescription : OK
Content           : /**
                     * @license
                     * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
                     * This code may only be used under the BSD style license found at
                     * http://polymer.github.io/LICENSE.txt
                     * Th...
RawContent        : HTTP/1.1 200 OK
                    Accept-Ranges: bytes
                    Content-Length: 8925
                    Content-Type: text/plain; charset=utf-8
                    Date: Thu, 26 Sep 2019 11:38:23 GMT
                    Last-Modified: Sat, 26 Oct 1985 08:15:00 GMT

                    /**
                     * @licen...
Forms             : {}
Headers           : {[Accept-Ranges, bytes], [Content-Length, 8925], [Content-Type, text/plain; charset=utf-8], [Date,
                    Thu, 26 Sep 2019 11:38:23 GMT]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 8925

注意内容类型!!!

【问题讨论】:

  • 您的问题分析有问题。 FileServer 在内部使用 ServeContent,而 ServeContent 为您管理内容类型。 golang.org/pkg/net/http/#ServeContent 可以试试 curl js 资源并分享结果吗?
  • 好的,看看我的更新

标签: javascript go gorilla polymer-3.x lit-element


【解决方案1】:

你确定你到达了正确的终点吗?

看那个小例子(你可以在你的主机上试一试)

$ tree
.
├── main.go
└── wwwroot
    └── test.js

1 directory, 2 files

$ cat main.go 
package main

import (
    "net/http"

    "github.com/gorilla/mux"
)

func main() {
    mux := mux.NewRouter()
    mux.PathPrefix("/").Handler(http.FileServer(http.Dir("./wwwroot")))

    http.ListenAndServe(":8080", mux)
}


$  cat wwwroot/test.js

$ go run main.go &
[1] 11841
$ curl -v http://localhost:8080/test.js
*   Trying ::1:8080...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /test.js HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.65.3
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 0
< Content-Type: application/javascript
< Last-Modified: Thu, 26 Sep 2019 12:12:15 GMT
< Date: Thu, 26 Sep 2019 12:15:36 GMT
< 
* Connection #0 to host localhost left intact

【讨论】:

  • 感谢这个例子!这太疯狂了:你的代码在我这边产生 Content-Type: text/plain;字符集=utf-8。一旦我将扩展名更改为 mjs,它就会正确报告 Content-Type: application/javascript。我检查了当前 http 和 mime 包的源代码。 Windows 和 Linux mime 检测之间似乎存在差异。在我的 Windows 注册表中,我发现扩展名 js 映射到 text/plain !!!其他 Windows 系统似乎没有该条目。今晚我要仔细调查一下……
  • 这确实非常令人惊讶。查看 go 源代码,ServeContent 调用 mime.TypeByExtension (golang.org/src/net/http/fs.go#L196)。在 Windows 上(为了使评论简短),它确实依赖于注册表,golang.org/src/mime/type_windows.go
  • 好的,注册表设置确实是问题所在。我不明白是哪个软件制作了这个映射,以及为什么这在其他应用程序之前没有引起任何问题。无论如何,感谢您的时间,我会接受您的回答,因为它让我知道如何诊断问题。
【解决方案2】:

在我的项目中,我遇到了同样的问题。以下是我的解决方案: 在tsconfig.json 文件中,制作"target": "es5"

【讨论】:

  • 不幸的是,这对我不起作用,因为我至少需要 es6。而且,顺便说一下,我在这个项目中没有使用 Typescript。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2020-03-14
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多