[英]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.