在運行我的 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>
uj5u.com熱心網友回復:
您的鏈接:
href="public/style.css"
不會與
app.use(express.static('public'));
因為這將在您的服務器檔案系統中查找public目錄,public/style.css這意味著它實際上正在尋找public/public/style.css,我假設您沒有以這種方式設定檔案系統。您沒有準確顯示style.css服務器檔案系統中的位置,但如果它直接位于public您指向的目錄express.static()中,那么您應該使用:
href="/style.css"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363251.html
標籤:javascript html css 表达
