'Http: // localhost: 3000 / style.css' से शैली लागू करने से इनकार कर दिया क्योंकि इसका MIME प्रकार ('पाठ / html') [बंद]
जब मैं अपने क्लाइंट सर्वर JS एप्लिकेशन को चलाता हूं तो मुझे यह त्रुटि होती है
DevTools में अवधारणा त्रुटि:
Refused to apply style from 'http://localhost:3000/style.css' because its MIME type
('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
क्लाइंट कोड:
const postData = async ( url = '', data = {})=>{
console.log(data);
const response = await fetch(url, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
// Body data type must match "Content-Type" header
body: JSON.stringify(data),
});
try {
const newData = await response.json();
console.log(newData);
return newData;
}catch(error) {
console.log("error", error);
}
}
postData('/addMovie', {answer:42});
सर्वर कोड:
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
app.use(bodyParser.urlencoded({extended : false}));
app.use(bodyParser.json());
const cors = require('cors');
app.use(cors());
app.use(express.static('website'))
const port = 3000
app.listen(port, getServerPortInfo)
function getServerPortInfo() {
console.log("Server listening at port " + port)
}
const data = []
app.post('/addMovie', addMovie)
function addMovie (req, res){
console.log(req.body)
console.log("here")
data.push(req.body)
console.log(data)
res.send(data)
}
HTML फ़ाइल:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Weather Journal</title>
<link href="https://fonts.googleapis.com/css?family=Oswald:400,600,700|Ranga:400,700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>
<button id="generate" type = "submit"> Generate </button>
</div>
<script src="app.js" type="text/javascript"></script>
</body>
</html>
क्या आप कृपया कुछ लिंक, सलाह, या संकेत प्रदान कर सकते हैं जो मुझे एक समाधान की ओर निर्देशित करने में मदद करेंगे?
अजीब बात यह है कि मेरे पास सीएसएस फाइल नहीं है।
कृपया ध्यान दें कि मैं पहले ही इस पद से गुजर चुका हूं और इसने मेरे मामले में मदद नहीं की है: क्रोम त्रुटि का कंसोल: शैली लागू करने से इनकार कर दिया क्योंकि इसका MIME प्रकार ('पाठ / html')
जवाब
आपने निर्दिष्ट किया है:
<link rel="stylesheet" href="style.css">
लेकिन आपके पास एक style.css फ़ाइल नहीं है । के लिए अनुरोध http://localhost:3000/style.css
में 404 त्रुटि हुई है, और आपके सर्वर ने एक HTML प्रतिक्रिया (mimetype है text/html
) की सेवा की है। त्रुटि यह सब कहती है:
शैली को '
http://localhost:3000/style.css
' से लागू करने से मना कर दिया गया क्योंकि इसका MIME प्रकार ('टेक्स्ट / html') एक समर्थित स्टाइलशीट MIME प्रकार नहीं है, और सख्त MIME जाँच सक्षम है।
समस्या है, वास्तविक के लिए या तो लिंक को ठीक करने के style.css एक पूर्ण यूआरएल के साथ, या पूरी तरह से स्टाइलशीट लिंक निकाल देते हैं, तो ऐसी कोई style.css संसाधन कहीं भी मौजूद है।
आपको फ़ाइल के माइम प्रकार को बनाए रखने के लिए फ़ोल्डर को स्थिर मानने के लिए अपने एक्सप्रेस ऐप को स्पष्ट रूप से बताना होगा।
कुछ इस तरह:
app.use(express.static("./"));