在运行我的HTML+ExpressJS代码时,我遇到了一个与CSS有关的问题。当我转到浏览器中的开发工具时,我看到Refused to apply style from 'https://localhost:5000/public/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
代码:
ExpressJS
const app = express();
app.use(express.static('public'));
app.get('/', (req,res) => {
res.sendFile('/home/pi/website/index.html');
});
app.listen(5000, () => console.log('http://localhost:5000'));HTML:
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title>homepage</title>
<link rel="stylesheet" type="text/css" href="public/style.css" media="screen" />
</head>
<body>
<h1 class=title>Hello!</h1>
<h1 class=title>This page is running NodeJS on a Raspberry Pi 4 4GB</h1>
</body>
</html>发布于 2021-11-21 04:24:21
你的链接:
href="public/style.css"将不能与
app.use(express.static('public'));因为这将在服务器文件系统中的public目录中查找public/style.css,这意味着它实际上正在查找public/public/style.css,我假设您没有以这种方式设置文件系统。您没有显示style.css在您的服务器文件系统中的确切位置,但如果它直接位于您指向express.static()的public目录中,那么您应该使用:
href="/style.css"https://stackoverflow.com/questions/70051319
复制相似问题