Javascript 拒绝应用来自'的样式;http://localhost:3000/style.css' 因为它的MIME类型(';text/html';)

Javascript 拒绝应用来自'的样式;http://localhost:3000/style.css' 因为它的MIME类型(';text/html';),javascript,html,jquery,node.js,http-post,Javascript,Html,Jquery,Node.js,Http Post,我在运行客户机-服务器JS应用程序时出现此错误 DevTools中的控制台错误: Refused to apply style from 'http://localhost:3000/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled. 客户端代码: const postData = async (

我在运行客户机-服务器JS应用程序时出现此错误

DevTools中的控制台错误:

Refused to apply style from 'http://localhost:3000/style.css' because its MIME type 
('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
客户端代码:

const postData = async ( url = '', data = {})=>{
console.log(data);
const response = await fetch(url, {
  method: 'POST', 
  credentials: 'same-origin',
  headers: {
      'Content-Type': 'application/json',
  },
 // Body data type must match "Content-Type" header        
  body: JSON.stringify(data), 
});

  try {
    const newData = await response.json();
    console.log(newData);
    return newData;
  }catch(error) {
  console.log("error", error);
  }
 }

postData('/addMovie', {answer:42});
服务器代码:

const express = require('express')
const bodyParser = require('body-parser');
const app = express()

app.use(bodyParser.urlencoded({extended : false}));
app.use(bodyParser.json());

const cors = require('cors');
app.use(cors());
app.use(express.static('website'))

const port = 3000
app.listen(port, getServerPortInfo)
function getServerPortInfo() {
    console.log("Server listening at port " + port)
}

const data = []
app.post('/addMovie', addMovie)

function addMovie (req, res){
    console.log(req.body)
    console.log("here")
    data.push(req.body)
    console.log(data)
    res.send(data)

 }
HTML文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Weather Journal</title>
    <link href="https://fonts.googleapis.com/css?family=Oswald:400,600,700|Ranga:400,700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
</head>
<body>    
    <div>
        <button id="generate" type = "submit"> Generate </button>
    </div>
    <script src="app.js" type="text/javascript"></script>
</body>
</html>

天气杂志
生成
您能否提供一些链接、建议或指针,帮助我找到解决方案

奇怪的是我没有css文件

请注意,我已经阅读了这篇文章,它对我的情况没有帮助: 您已指定:

<link rel="stylesheet" href="style.css">

但是您没有style.css文件。请求
http://localhost:3000/style.css
已导致404错误,并且您的服务器已提供HTML响应(mimetype为
text/HTML
)。错误说明了一切:

拒绝应用来自“
http://localhost:3000/style.css因为
其MIME类型('text/html')不是受支持的样式表MIME类型,
并且启用了严格的MIME检查


纠正问题,要么链接到实际的样式。CSS带有完整的URL,要么完全删除样式表链接,如果没有这样的样式。任何地方都存在CSS资源。

< p>您需要明确地告诉您的Express应用程序将文件夹视为静态,以保留文件的MIME类型。

比如:

app.use(express.static("./"));

你能不能也检查一下是否相关?在这里,css获取实际上会产生404。您是否在浏览器开发人员工具(网络选项卡)中检查了资源是否已正确获取?等等…只是重复了一次。你说奇怪的是我没有css文件。但是你显然是在用
链接一个,那是怎么回事?如果您知道没有css文件,但正在尝试获取一个,那么您应该知道404即将到来。我误解你的评论了吗?那是谷歌的css,虽然它不是本地的。这不应该有问题,对吧?不,不是。这是资源相对路径。如果您在
http://localhost:3000/
然后
style.css
就是
http://localhost:3000/style.css
。具体来说,不是从google.com获取的东西,您需要指定style.css资源所在位置的完整url。啊,非常感谢。这就解决了问题