从 webpack 加载 bundle.js 文件将 mime 类型更改为 text/html [英] loading a bundle.js file from webpack changes mime type to text/html
问题描述
我正在尝试使用 react-redux 和 express 为服务器实现应用程序的服务器端渲染,正在使用 webpack 来创建包.
I am trying to achieve server side rendering for an app using react-redux and express for the server, webpack is being used to create the bundle.
我开始使用以下文档:
https://redux.js.org/docs/recipes/ServerRendering.html
我有以下设置来加载从 webpack 创建的 main.js:
I have the following set up to load up the main.js created from webpack:
<script type="application/javascript" src="/static/main.js"></script>
不过,在启动 express 服务器时,这是我在控制台中看到的:
On firing up the express server though, this is what I see in the console:
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.
这不会在任何浏览器中启动.知道会发生什么吗?
This is not firing up in any browser. Any idea what might be going on?
推荐答案
在我看来是配置问题.
如果它返回 MIME 类型 ('text/html')
这意味着,在该特定路径上它无法找到/解析 main.js
文件,因此它返回一个默认的 404 html 页面,该页面被严格定义为 javascript.
If it is returning MIME type ('text/html')
that means, at that specific path it is not able to find/resolve the main.js
file, so it is returning a default 404 html page instead, which is strictly defined to be javascript.
因此,理想情况下,您可以将快速服务器配置为提供静态文件,如下所示.
So, Ideally what you can do is that you can configure your express server to serve static files as shown below.
app.use(express.static(__dirname + "/static"));
更多细节可以参考::-> https://expressjs.com/en/starter/static-files.html
For more details, you can refer to::-> https://expressjs.com/en/starter/static-files.html
快乐编码!
这篇关于从 webpack 加载 bundle.js 文件将 mime 类型更改为 text/html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!