'Http: // localhost: 3000 / style.css'den stil uygulama reddedildi çünkü MIME türü (' text / html ') [kapalı]

Dec 20 2020

İstemci sunucusu JS uygulamamı çalıştırdığımda bu hatayı alıyorum

DevTools'DAKİ KONSOL HATASI:

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.

MÜŞTERİ KODU:

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});

SUNUCU KODU:

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 DOSYASI:

<!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>

Beni bir çözüme yönlendirmeye yardımcı olacak bazı bağlantılar, tavsiyeler veya işaretler verebilir misiniz?

Garip olan şey, benim bir css dosyamın olmaması.

Lütfen bu gönderiyi zaten incelediğimi ve durumuma yardımcı olmadığını unutmayın: Chrome konsolu hatası: MIME türü ('text / html') nedeniyle stili uygulamayı reddetti

Yanıtlar

1 Wyck Dec 20 2020 at 08:15

Belirttiniz:

<link rel="stylesheet" href="style.css">

Ancak bir style.css dosyanız yok. İsteği http://localhost:3000/style.css404 hatasıyla sonuçlandı ve sunucunuz bir HTML yanıtı sundu (mimetype is text/html). Hata her şeyi söylüyor:

http://localhost:3000/style.cssMIME türü ('metin / html') desteklenen bir stil sayfası MIME türü olmadığı ve sıkı MIME denetimi etkinleştirildiği için ' ' kaynağından stil uygulama reddedildi .

Sorunu düzeltmek için ya gerçek style.css'ye tam URL ile bağlantı verin ya da hiçbir yerde böyle bir style.css kaynağı yoksa stil sayfası bağlantısını tamamen kaldırın .

Zahid Dec 20 2020 at 08:21

Dosyanın mime türünü korumak için ekspres uygulamanıza klasörü statik olarak kabul etmesini açıkça söylemeniz gerekir.

Gibi bir şey:

app.use(express.static("./"));