【问题标题】:Refused to apply style from 'http://localhost:3000/assets/styles/signup.css' because its MIME type('text/html')is not a supported stylesheet MIME type拒绝应用来自 'http://localhost:3000/assets/styles/signup.css' 的样式,因为它的 MIME 类型('text/html')不是受支持的样式表 MIME 类型
【发布时间】:2021-04-01 12:08:18
【问题描述】:

有人问了类似的问题,我又问了在阅读了许多问题 (including this most upvoted) 之后,尝试并没有得到任何积极的结果,并且敲了我大约两个小时的头。请调查一下。我正在学习一些网络开发。我正在尝试构建这个bootstrap signup page。但是我被困在非常早期的阶段。附上代码文件:(在Gofile.io上传的zip文件,链接在最后)

我在 chrome 控制台上遇到错误:

Refused to apply style from 'http://localhost:3000/assets/styles/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
localhost/:1 Refused to apply style from 'http://localhost:3000/assets/styles/signup.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
localhost/:39 GET http://localhost:3000/assets/images/bootstrap-solid.svg 404 (Not Found)

在打开任何指向新标签的链接时,我得到了(bootsrap-image 的示例):

Cannot GET /assets/images/bootstrap-solid.svg

我在 Windows 10 操作系统上使用 Google Chrome 最新版本。

除了 package.jsonapp.js 之外,我已经复制了所有 html 和 css 引导示例源代码。

directory structure--

Newsletter-signup
|_assets
|      |_images
|      |    |_bootstrap-solid.svg
|      |    |_favicon.ico
|      |
|      |_styles
|            |_bootstrap.min.css
|            |_signup.css
|
|_app.js
|_package.json
|_signup.html

package.json:

{
  "name": "Newsletter-Signup",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "type": "module",
  "scripts": {
    "start": "nodemon app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.7"
  }
}

app.js:

import express from "express";
import https from "https";
import path from "path";

const app = express();
const __dirname = path.resolve();
const portNumber = 3000;
// console.log(__dirname);

app.use(express.static(path.join(__dirname, "assets")));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname, "signup.html"));
});

app.post("/", () => {
  console.log("called app.post method");
});

app.listen(portNumber, () => {
  console.log("server listening at portNumber: " + portNumber);
});

signup.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="description" content="" />
    <meta name="author" content="" />
    <link rel="icon" href="assets/images/favicon.ico" />

    <title>Signup Form</title>

    <!-- <link
      rel="canonical"
      href="https://getbootstrap.com/docs/4.0/examples/sign-in/"
    /> -->

    <!-- Bootstrap core CSS -->
    <link
      type="text/css"
      rel="stylesheet"
      href="assets/styles/bootstrap.min.css"
    />

    <!-- Custom styles for this template -->
    <link type="text/css" rel="stylesheet" href="assets/styles/signup.css" />
  </head>

  <body class="text-center">
    <form class="form-signin">
      <img
        class="mb-4"
        src="assets/images/bootstrap-solid.svg"
        alt=""
        width="72"
        height="72"
      />
      <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
      <label for="inputEmail" class="sr-only">Email address</label>
      <input
        type="email"
        id="inputEmail"
        class="form-control"
        placeholder="Email address"
        required
        autofocus
      />
      <label for="inputPassword" class="sr-only">Password</label>
      <input
        type="password"
        id="inputPassword"
        class="form-control"
        placeholder="Password"
        required
      />
      <div class="checkbox mb-3">
        <label>
          <input type="checkbox" value="remember-me" /> Remember me
        </label>
      </div>
      <button class="btn btn-lg btn-primary btn-block" type="submit">
        Sign in
      </button>
      <p class="mt-5 mb-3 text-muted">&copy; 2017-2018</p>
    </form>
  </body>
</html>

signup.css

body {
  height: 100%;
}

body {
  display: -ms-flexbox;
  display: -webkit-box;
  display: flex;
  -ms-flex-align: center;
  -ms-flex-pack: center;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: #f5f5f5;
}

.form-signin {
  width: 100%;
  max-width: 330px;
  padding: 15px;
  margin: 0 auto;
}
.form-signin .checkbox {
  font-weight: 400;
}
.form-signin .form-control {
  position: relative;
  box-sizing: border-box;
  height: auto;
  padding: 10px;
  font-size: 16px;
}
.form-signin .form-control:focus {
  z-index: 2;
}
.form-signin input[type="email"] {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

如果我使用 CDN 进行引导,则引导部分可以工作。 另外,如果没有节点,如果我只是在浏览器中粘贴 signup.html 的路径,它工作正常。

您可以从here下载并查看完整的源文件

【问题讨论】:

  • 您在浏览器开发工具的“网络”选项卡中看到了什么?

标签: javascript html css node.js express


【解决方案1】:

目前,对http://localhost:3000/assets/styles/signup.css 的浏览器请求会返回一个带有“text/html”内容的 404 页面(您可以在浏览器的开发者工具中查看)。
这就是你收到错误MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

从您的资产 URL 中删除“资产”字样:

<link type="text/css" rel="stylesheet" href="/styles/signup.css" />

此外,您可以通过在浏览器选项卡输入中粘贴完整的 CSS 链接来检查它是否有效。

【讨论】:

  • 哦,伙计,这立即奏效了 :D !!!万分感谢!顺便说一句,我只是好奇你是怎么想到的?为什么它甚至不写静态文件的完整路径就可以工作?
  • @T.M15 你用"assets" 静态目录配置了express.static,并且“Express 查找相对于静态目录的文件,因此静态目录的名称不是 URL 的一部分。 " 来自official docs
【解决方案2】:

尝试将 html 代码中的 url 更改为类似

 assets/styles/bootstrap.min.css

 /assets/styles/bootstrap.min.css

然后查看您的“网络”标签,看看发生了什么。通常这些“拒绝应用样式”错误消息会突然出现,因为浏览器响应的是 404 页面而不是您想要的样式表。

【讨论】:

  • 耶!早些时候也尝试过,但结果是否定的。顺便说一句,这个问题现在已经解决了,虽然我会记住检查网络选项卡以供将来调试的建议。感谢您抽出宝贵时间:)
猜你喜欢
  • 2018-12-11
  • 2020-11-10
  • 1970-01-01
  • 2021-11-29
  • 2021-06-04
  • 2018-12-14
  • 2018-10-24
  • 2021-03-30
  • 2018-08-30
相关资源
最近更新 更多