简体   繁体   English

NodeJS,MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型

[英]NodeJS, MIME type ('text/html') is not a supported stylesheet MIME type

Everytime I connect to my local server on Google Chrome, I get this error :每次我在 Google Chrome 上连接到本地服务器时,都会收到此错误:

Refused to apply style from 'http://localhost:2000/app.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

I can access my css file by typing its path so the problem doesn't come from there.我可以通过键入其路径来访问我的 css 文件,因此问题不会来自那里。 I also searched for informations about this topic but nothing worked.我还搜索了有关此主题的信息,但没有任何效果。

Here's my code这是我的代码

Server Side服务器端

const Game = require("./class/game");
const express = require("express");
const app = express();
const serv = require("http").Server(app);

// Server
app.get("/", function(req, res) {
    res.sendFile(__dirname + "/client/index.html");
});
app.use("/client", express.static(__dirname + "/client"));

serv.listen(2000);
console.log("Server started on port 2000");


var io = require("socket.io")(serv, {});
io.sockets.on("connection", function(socket) {
    console.log("Connection done");
});

Client side客户端

HTML HTML

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
    <link rel="stylesheet" type="text/css" href="./app.css">
    <title>Games</title>
</head>

<script>
    var socket = io();
</script>

<body>
    test
</body>

</html>

CSS CSS

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background-color: aquamarine;
}

app.css should be served from /client dir. app.css 应该从/client目录提供。 Change href as below更改href如下

href="/client/app.css">

In your case browser is not able to found the app.css and browser might be receiving a similar response as below在您的情况下,浏览器无法找到 app.css 并且浏览器可能会收到如下类似的响应在此处输入图片说明

Which is clearly not a valid response and Mime-type for CSS file.这显然不是 CSS 文件的有效响应和 Mime 类型。 That's why you are getting the above error.这就是您收到上述错误的原因。

As you have defined static content to be served as below由于您已定义要提供的静态内容,如下所示

app.use("/client", express.static(__dirname + "/client"));

Changing href /app.css to /client/app.css will solve your issue provided app.css is present in that directory.href /app.css更改为/client/app.css将解决您的问题,前提是该目录中存在 app.css。

暂无
暂无

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

相关问题 MIME 类型 (&#39;text/html&#39;) 不是受支持的样式表 MIME 类型 - MIME type ('text/html') is not a supported stylesheet MIME type ExpressJS 错误:MIME 类型('text/html')不是受支持的样式表 MIME 类型 - ExpressJS error: MIME type ('text/html') is not a supported stylesheet MIME type 拒绝应用样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型 - Refused to apply style because its MIME type ('text/html') is not a supported stylesheet MIME type 拒绝应用来自 '<url> ' 因为它的 MIME 类型('text/html')不是受支持的样式表 MIME 类型</url> - Refused to apply style from '<URL>' because its MIME type ('text/html') is not a supported stylesheet MIME type 不支持MIME类型(&#39;text / html&#39;) - MIME type ('text/html') is not a supported 拒绝应用{filename}中的样式,因为它的MIME类型(&#39;text / html&#39;)不是受支持的样式表MIME类型 - Refused to apply style from {filename} because its MIME type ('text/html') is not a supported stylesheet MIME type Django:拒绝从...应用样式,因为它的 MIME 类型(&#39;text/html&#39;)不是受支持的样式表 MIME 类型 - Django: Refused to apply style from ... because its MIME type ('text/html') is not a supported stylesheet MIME type 拒绝应用“ <URL> &#39;,因为它的MIME类型(&#39;text / html&#39;)不是受支持的样式表MIME类型,并且启用了严格的MIME检查 - Refused to apply style from '<URL>' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled 由于MIME类型为text / html,因此未加载样式表 - Stylesheet not loading because MIME type is text/html 拒绝应用来自“xxx/style.css”的样式,因为它的 MIME 类型(“text/html”)不是受支持的样式表 MIME 类型 - Refused to apply style from 'xxx/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM