Weigerte sich, Stil von 'http: // localhost: 3000 / style.css' anzuwenden, weil sein MIME-Typ ('text / html') [geschlossen]

Dec 20 2020

Ich habe diesen Fehler, wenn ich meine Client-Server-JS-Anwendung ausführe

KONSOLENFEHLER IN 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.

KUNDENCODE:

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

SERVERCODE:

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

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

Könnten Sie bitte einige Links, Ratschläge oder Hinweise geben, die mir helfen würden, eine Lösung zu finden?

Das Seltsame ist, dass ich keine CSS-Datei habe.

Bitte beachten Sie, dass ich diesen Beitrag bereits durchgesehen habe und er meinem Fall nicht geholfen hat: Die Konsole von Chrome Fehler: Die Anwendung des Stils wurde abgelehnt, da der MIME-Typ ('text / html')

Antworten

1 Wyck Dec 20 2020 at 08:15

Sie haben angegeben:

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

Sie haben jedoch keine style.css- Datei. Die Anforderung für http://localhost:3000/style.csshat zu einem 404-Fehler geführt, und Ihr Server hat eine HTML-Antwort bereitgestellt (Mimetyp ist text/html). Der Fehler sagt alles:

Die Anwendung des Stils von ' http://localhost:3000/style.css' wurde abgelehnt , da der MIME-Typ ('text / html') kein unterstützter Stylesheet-MIME-Typ ist und die strikte MIME-Prüfung aktiviert ist.

Um das Problem zu beheben, verknüpfen Sie entweder die vollständige style.css mit einer vollständigen URL oder entfernen Sie den Stylesheet-Link vollständig, wenn nirgendwo eine solche style.css- Ressource vorhanden ist.

Zahid Dec 20 2020 at 08:21

Sie müssen Ihre Express-App explizit anweisen, den Ordner als statisch zu betrachten, um den MIME-Typ der Datei beizubehalten.

Etwas wie:

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