节点拒绝应用样式,因为其MIME类型('text/html')不是受支持的样式表MIME [英] Node refused to apply style because its MIME type ('text/html') is not a supported stylesheet MIME

查看:80
本文介绍了节点拒绝应用样式,因为其MIME类型('text/html')不是受支持的样式表MIME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am going to server an static index.html with main.css using this node server:

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>

main.css

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

结构:

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

当我加载 index.html 时,css已加载,但是在fron节点上提供服务,我得到:

When I load index.html the css is loaded, but serving it fron node, I get:

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" 有用.

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

What could be wrong here? How can I fix it?

推荐答案

替换此行

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

使用

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

这篇关于节点拒绝应用样式,因为其MIME类型('text/html')不是受支持的样式表MIME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆