Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 因为它的MIME类型(‘text / html’)不可执行,并且启用了严格的MIME类型检查大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Nodejs和webpack4,我正在尝试将main.js文件链接到index.html.我在网上尝试了所有可能的解决方案它们似乎都不适用于我..我是新手建议欢迎,请让我知道我做错了什么.

这是我所看到的错误日志:

GET http://localhost:3000/dist/main.js net::ERR_ABORTED
localhost/:1
Refused to execute script from 'http://localhost:3000/dist/main.js' 
because its MIME type ('text/html') is not executable,and Strict MIME type checking is enabled.@H_301_14@ 
 

公共/ index.html的

<!DOCTYPE html>
<html>
<head>
    <Meta charset="utf-8">
    <Meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title> 
</head>
<body>

    <form action="/" method="POST" accept-charset="utf-8">
        <input type="email" name="email" placeholder="Email">
        <input type="submit" name="submit">
    </form>

    <script type="text/javascript" src="dist/main.js"> </script>

</body>
</html>@H_301_14@ 
 

/app.js

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

//Middleware
app.use(express.static(__dirname + '/public' ));

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

app.listen(3000,function(){
    console.log('Server running on port 3000');
});@H_301_14@ 
 

文件结构

news_letter //Root directory
|_ dist
|   |_ main.js
|_ public
|   |_ index.html
|_ src
|   |_ index.js
|_ app.js@H_301_14@

解决方法

您需要main.js文件位于公用文件夹中.

看起来你把main.js文件放在dist / folder和index.html文件中的public /文件夹中.

但是你只在app.js文件中设置一个目录作为“静态文件目录”

app.use(express.static(__dirname + '/public' ));@H_301_14@ 
 

html中的每个路径都相对于公用文件夹中的路径.因此,将您的dist /文件夹移动到公共/文件夹中,一切都应该有效

文件结构

news_letter //Root directory
|_ public
|   |_ dist
|   |   |_ main.js
|   |_ index.html
|_ src
|   |_ index.js
|_ app.js@H_301_14@

大佬总结

以上是大佬教程为你收集整理的node.js – 因为它的MIME类型(‘text / html’)不可执行,并且启用了严格的MIME类型检查全部内容,希望文章能够帮你解决node.js – 因为它的MIME类型(‘text / html’)不可执行,并且启用了严格的MIME类型检查所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。