【问题标题】:Refused to apply style from 'URL' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled拒绝应用来自 'URL' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查
【发布时间】:2020-07-09 03:11:27
【问题描述】:

这是我的堆栈

  • 快递
  • Ejs
  • Sass(由 'npm install --save-dev sass' 安装 - 一个开发依赖项)

我收到一个错误

拒绝应用来自 'http://localhost:3000/css/style.css' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查。


我的 head 标签位于单独的部分文件夹中:

- 意见
- - 部分
- - - header.ejs
-- index.ejs

我的 CSS 也在另一个文件夹中

- 公开
- - css
- - - style.css
- - - style.css.map
- - - style.scss


我的头像是这样的:

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Learn Web</title>
<base href="/">
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>

我这样渲染我的索引:

app.get('/', (req, res) => {
    res.render('index');
})

我尝试添加&lt;base href="/"&gt;
我试图省略rel="stylesheet"
我尝试将type="text/css" 更改为type="text/html" - 它消除了错误但样式不适用。


提前谢谢你!

【问题讨论】:

  • 这能回答你的问题吗? Stylesheet not loaded because of MIME-type
  • http://localhost:3000/css/style.css 实际上是在为 CSS 文档提供服务,而不是例如HTML 错误消息?
  • "我这样渲染我的索引:" — 你如何渲染 CSS?
  • ("我这样渲染我的索引:" — 你如何渲染 CSS?) - 使用

标签: html css express sass ejs


【解决方案1】:

将此行添加到您的 js 文件将允许本地 css 链接: app.use(express.static(__dirname));

您可以在此处阅读有关在 Express 中提供静态文件的信息: http://expressjs.com/en/starter/static-files.html

【讨论】:

    【解决方案2】:

    所以我解决了这个问题。但不知何故,它是一个半修复。因为当我遇到这个问题时,我才刚刚开始我的项目。我重新启动了我的项目,但是通过使用 express-generator 自动创建应用程序框架。之后,我使用 npm 安装了 Sass,并且成功了。

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 2020-01-02
      • 2018-11-21
      • 2018-08-01
      • 2018-07-31
      • 2018-08-24
      • 2021-07-09
      • 2021-06-20
      • 2021-06-05
      相关资源
      最近更新 更多