简体   繁体   中英

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

I am trying to add my webpack build.js file to my html page and I keep getting these errors in 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 config:

const path = require('path')

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

}

I have tried using the historyApiFallback: true and it didn't fix anything. I have read a lot about people fixing the issue in the HTML script tag being wrong, so maybe there is something I am not seeing. Hopefully someone can help me out! Thanks in advance.

Just to clarify this is where my files are Main Folder: C:\\Users\\jake\\Documents\\project\\CoffeeClub\\newsletter

  • Public
  • app.js
  • scripts.js
  • webpack.config.js

Inside Public I have these files:

  • build.js
  • index.html

You said:

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

So when the browser asks for /public/build.js the server gives it ./public/public/build.js . Since that doesn't exist, it gives it a 404 instead.

One public from the URL, the other from the static base dir.

If you want to include /public in the URL, then you have to limit the route of the middleware.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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