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

标签 html css node.js

我将使用此 Node 服务器为静态 index.htmlmain.css 服务器提供服务:

服务.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 已加载,但从 Node 提供它,我得到:

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" 没有有用。

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

最佳答案

替换这一行

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

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

关于html - Node 拒绝应用样式,因为它的 MIME 类型 ('text/html' ) 不是受支持的样式表 MIME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51201869/

相关文章:

javascript - 如何将 esimakin 分页与我的表格链接?

html - 更改动画响应按钮的颜色

html - 首次加载时 Bootstrap Navheader Safari 奇怪的布局

node.js - 按标题而非 ID 搜索 MongoDB

node.js - 获取区域 EC2 部署到使用 Node

javascript - 通过 CSS 缩放网页上的所有元素

javascript - 在将按键事件应用于 DOM 之前,如何确定在按键事件之后的值是什么?

html - 溢出-x : scroll doesn't seem to work

javascript - 如何从保存在数组中的元素中更改某个值?

javascript - QuickPay Node.js REST api