登录
首页 >  Golang >  Go问答

模块化开发:Golang vs JavaScript

来源:stackoverflow

时间:2024-03-09 09:15:30 236浏览 收藏

本篇文章向大家介绍《模块化开发:Golang vs JavaScript》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

我想将 polymer litelement 与 go 后端结合使用。 通过 litelement,我在 javascript 模块中实现了 web 组件! 对于服务器端的路由,我使用 gorilla mux,如下所示

mux := mux.newrouter()
mux.pathprefix("/").handler(http.fileserver(http.dir("./wwwroot")))

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

无法加载模块脚本:服务器以非 javascript mime 类型“text/plain”进行响应。根据 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

注意内容类型!!!


解决方案


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

查看那个小示例(您可以尝试在主机上进行检查)

$ 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

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

到这里,我们也就讲完了《模块化开发:Golang vs JavaScript》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>