繁体   English   中英

MIME类型('text / html')不是Chrome中的可执行错误

[英]MIME type ('text/html') is not executable errors in Chrome

我正在尝试将webpack build.js文件添加到我的html页面中,并且在Chrome中不断收到这些错误:

GET http://localhost:3000/public/build.js net::ERR_ABORTED 404 (Not Found)
Refused to execute script from 'http://localhost:3000/public/build.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

app.js:

const express = require('express');
const app = express();

app.use(express.static('public'));

app.listen(3000, function() {
    console.log('poopy');
});

app.post('/', function(req, res) {
    res.end('success');
});

index.html的:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Coffee Newsletter</title>
</head>
<body>
    <form action="/" method="post">
        <input type="email" name="email">
        <input type="submit" name="submit">
    </form>
    <script type="text/javascript" src="./public/build.js"></script>
</body>
</html>

webpack配置:

const path = require('path')

module.exports = {
    entry: './scripts.js',
    output: {
        path: __dirname + '/public',
        filename: 'build.js'
    },
    watch: true

}

我试过使用historyApiFallback: true ,它没有解决任何问题。 我读过很多关于人们解决HTML脚本标签错误的问题的信息,所以也许有些事情我没有看到。 希望有人可以帮助我! 提前致谢。

只是为了澄清这是我的文件所在的主文件夹: C:\\ Users \\ jake \\ Documents \\ project \\ CoffeeClub \\ newsletter

  • 上市
  • app.js
  • scripts.js中
  • webpack.config.js

在Public内部,我有以下文件:

  • build.js
  • 的index.html

你说:

 app.use(express.static('public')); 

因此,当浏览器要求输入/public/build.js ,服务器会为其提供./public/public/build.js 由于该名称不存在,因此改为提供404。

一个来自URL,另一个来自静态基目录。

如果要在URL中包含/public ,则必须限制中间件的路由。

app.use('/public', express.static('public'));

在发现服务器拉出HTML的问题后,我删除了多余的public目录,并将其全部放在根目录中,并且现在可以正常工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM