【问题标题】:loading a bundle.js file from webpack changes mime type to text/html从 webpack 加载 bundle.js 文件将 mime 类型更改为 text/html
【发布时间】:2018-06-17 20:45:31
【问题描述】:

我正在尝试使用 react-redux 为应用程序实现服务器端渲染,并为服务器提供 express,正在使用 webpack 创建包。

我开始使用以下文档:

https://redux.js.org/docs/recipes/ServerRendering.html

我有以下设置来加载从 webpack 创建的 main.js:

<script type="application/javascript" src="/static/main.js"></script>

不过,在启动 express 服务器时,这是我在控制台中看到的:

Refused to execute script from 'http://localhost:8080/static/main.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

这不会在任何浏览器中启动。知道会发生什么吗?

【问题讨论】:

标签: javascript express react-redux server-side-rendering serverside-javascript


【解决方案1】:

在我看来是配置问题。

如果它返回MIME type ('text/html'),这意味着在该特定路径上它无法找到/解析main.js 文件,因此它会返回一个默认的404 html 页面,该页面被严格定义为javascript。

因此,理想情况下,您可以配置快速服务器以提供静态文件,如下所示。

app.use(express.static(__dirname + "/static"));

更多详情可以参考::->https://expressjs.com/en/starter/static-files.html

 

编码愉快!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-19
    • 2017-09-17
    • 2019-12-21
    • 2018-03-30
    • 2021-12-29
    • 2021-03-17
    • 2018-06-14
    相关资源
    最近更新 更多