【问题标题】:Node refused to apply style because its MIME type ('text/html') is not a supported stylesheet MIME节点拒绝应用样式,因为它的 MIME 类型('text/html')不是受支持的样式表 MIME
【发布时间】:2018-12-14 13:25:40
【问题描述】:

我将使用此节点服务器为静态index.htmlmain.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')
})

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>

ma​​in.css

body {
  background: #ffdd;
  color: #eaeaea;
  padding: 10px;
}

结构:

project structure:
       index.html
       serve.js
       assets
             js
             css
                main.css

当我加载 index.html 时,css 已加载,但从节点提供它,我得到:

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.

我也尝试过href="/assets/css/main.css"href="./assets/css/main.css",但无济于事。

这里可能有什么问题?我该如何解决?

【问题讨论】:

  • @zer00ne 不确定该答案如何适用于我的情况,但将 css 文件名更改为 default.css 不起作用。
  • 您应该阅读它,Codisan 显然做到了。

标签: html css node.js


【解决方案1】:

替换这一行

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

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

【讨论】:

    【解决方案2】:

    我最近遇到了同样的问题。我在“/public”文件夹中有 index.html,在 /views 中有 index.ejs。它误导了 Node.js,所以我从 /public 文件夹中删除了 index.html 以及其他 html 页面。希望它能节省您的时间

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,并且经历了将近几个小时的麻烦,我注意到我必须使用这个:

      app.use(express.static(__dirname, "../public"));
      

      它应该可以完成这项工作,并希望能节省您的时间。

      【讨论】:

        猜你喜欢
        • 2020-11-10
        • 2021-06-04
        • 2018-10-24
        • 1970-01-01
        • 2021-11-29
        • 1970-01-01
        • 1970-01-01
        • 2018-12-11
        • 2018-11-25
        相关资源
        最近更新 更多