节点拒绝应用样式,因为其MIME类型('text / html')不是受支持的样式表MIME

Bab*_*abr 2 html css node.js

我将服务器的静态index.html与main.css使用此节点服务器:

serve.js:

var express = require('express')
var cors = require('cors')
var app = express()
var path = require('path');

app.use(cors())

app.use(express.static('assets'))

app.get('/', function (req, res, next) {
  res.sendFile(path.join(__dirname + '/index.html'));
})

app.listen(3000, function () {
  console.log('CORS-enabled web server listening on port 3000')
})
Run Code Online (Sandbox Code Playgroud)

该index.html的:

<!doctype html>
<html class="no-js" lang="">
    <head>
        <link rel="stylesheet" type="text/css" href="./assets/css/main.css">

    </head>
    <body>

        <p>Hello Html!</p>
        <div class="thejson"></div>


        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

main.css

body {
  background: #ffdd;
  color: #eaeaea;
  padding: 10px;
}
Run Code Online (Sandbox Code Playgroud)

结构:

project structure:
       index.html
       serve.js
       assets
             js
             css
                main.css
Run Code Online (Sandbox Code Playgroud)

当我加载index.htmlcss时,但是在fron节点上服务时,我得到:

Refused to apply style from 'http://127.0.0.1:3000/assets/css/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Run Code Online (Sandbox Code Playgroud)

我也试着href="/assets/css/main.css"和href="./assets/css/main.css"与无济于事。

这有什么问题吗?我该如何解决?

小智 7

替换这条线

app.use(express.static('assets'))
Run Code Online (Sandbox Code Playgroud)

与

app.use('/assets', express.static('assets'))
Run Code Online (Sandbox Code Playgroud)