NodeJS,MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型 [英] NodeJS, MIME type ('text/html') is not a supported stylesheet MIME type
问题描述
每次我在 Google Chrome 上连接到本地服务器时,都会收到此错误:
Everytime I connect to my local server on Google Chrome, I get this error :
拒绝应用来自 'http://localhost:2000/app.css' 的样式,因为其 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查.
我可以通过键入其路径来访问我的 css 文件,因此问题不会来自那里.我也搜索了有关此主题的信息,但没有任何效果.
I can access my css file by typing its path so the problem doesn't come from there. I also searched for informations about this topic but nothing worked.
这是我的代码
服务端
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");
});
客户端
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
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: aquamarine;
}
推荐答案
app.css 应该从 /client
目录提供.将href
改成如下
app.css should be served from /client
dir. Change href
as below
href="/client/app.css">
在您的情况下,浏览器无法找到 app.css 并且浏览器可能会收到如下类似的响应
In your case browser is not able to found the app.css and browser might be receiving a similar response as below
这显然不是 CSS 文件的有效响应和 Mime 类型.这就是您收到上述错误的原因.
Which is clearly not a valid response and Mime-type for CSS file. 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"));
将 href
/app.css
更改为 /client/app.css
将解决您的问题,前提是该目录中存在 app.css.
Changing href
/app.css
to /client/app.css
will solve your issue provided app.css is present in that directory.
这篇关于NodeJS,MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!