快速路由静态文件:加载模块脚本失败
Express routing static file: Failed to load module script
我的 Angular 9 应用程序与往常一样,这是我第一次遇到这个问题。
我不断收到以下错误:“无法加载模块脚本:服务器响应了非 JavaScript MIME 类型的“text/html”。
我在网上搜索了答案,但似乎找不到任何可以解决问题的方法。我所做的一切似乎都遵循给出的建议。我想知道这是否是 Angular v9 的新问题?
我的快递 app.js 位于我的 Angular 项目的另一个文件夹中。以下是我的文件夹结构:
客户端
- 距离
- index.html
中间层
- app.js
app.js:
app.use(express.static(__dirname + '/dist'));
app.get('*/', (req, res) => {
const indexFile = path.join(__dirname, '../client/dist/index.html');
res.sendFile(indexFile);
})
文件路径正确,我已经安慰记录了它,所有这些都加起来了。我也将 angular.json 中的输出路径更改为 dist 而不是另一个子文件夹。
我仍然收到此错误。有人可以帮忙吗?
为此你必须使用 express.static()
:
app.use(express.static(path.join(__dirname, '../client/dist')))
这里的问题是你总是用 index.html
文件响应,如果文件包含一些 <script>
标签,那么浏览器会期望收到一个 .js
文件,但你的服务器响应使用 .html
文件,这就是您收到错误的原因。
非常感谢 sebastian-kaczmarek 提供此解决方案。
有效答案如下:
app.use(express.static(path.join(__dirname, '../client/dist')))
感谢您的支持。
我的 Angular 9 应用程序与往常一样,这是我第一次遇到这个问题。
我不断收到以下错误:“无法加载模块脚本:服务器响应了非 JavaScript MIME 类型的“text/html”。
我在网上搜索了答案,但似乎找不到任何可以解决问题的方法。我所做的一切似乎都遵循给出的建议。我想知道这是否是 Angular v9 的新问题?
我的快递 app.js 位于我的 Angular 项目的另一个文件夹中。以下是我的文件夹结构:
客户端
- 距离
- index.html
- 距离
中间层
- app.js
app.js:
app.use(express.static(__dirname + '/dist'));
app.get('*/', (req, res) => {
const indexFile = path.join(__dirname, '../client/dist/index.html');
res.sendFile(indexFile);
})
文件路径正确,我已经安慰记录了它,所有这些都加起来了。我也将 angular.json 中的输出路径更改为 dist 而不是另一个子文件夹。
我仍然收到此错误。有人可以帮忙吗?
为此你必须使用 express.static()
:
app.use(express.static(path.join(__dirname, '../client/dist')))
这里的问题是你总是用 index.html
文件响应,如果文件包含一些 <script>
标签,那么浏览器会期望收到一个 .js
文件,但你的服务器响应使用 .html
文件,这就是您收到错误的原因。
非常感谢 sebastian-kaczmarek 提供此解决方案。
有效答案如下:
app.use(express.static(path.join(__dirname, '../client/dist')))
感谢您的支持。