【问题标题】:CSS not loading with node.js and expressCSS 没有加载 node.js 和 express
【发布时间】:2022-01-06 17:10:01
【问题描述】:

运行我的 HTML 时,我的 CSS 不会加载。 HTML 与 CSS 正确连接,但 express 和 node.js 似乎忽略了它。我看不懂文章和教程或其他堆栈溢出问题,所以我只发送代码。

我还收到一个错误,说 MIME 类型是 text/HTML,即使我已经检查了多次,MIME 类型确实是 text/CSS。

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Infinite Road</title>
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <div class="infinite">
        <div class="shadow"></div>
    </div>
</body>
</html>

CSS:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  body {
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      background: radial-gradient(#9bdfff, #009be4);
  }
  .infinite {
      position: relative;
      width: 800px;
      height: 160px;
      background: #525252;
      transform-origin: bottom;
      transform-style: preserve-3d;
      transform: perspective(500px) rotateX(30deg);
  }
  .infinite::before {
      content: "";
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      left: 0;
      width: 100%;
      height: 10px;
      background: linear-gradient(90deg, #fff 0%, #fff 70%, #525252 70%, #525252 100%);
      background-size: 120px;
      animation: animate 0.5s linear infinite;
  }
  @keyframes animate {
      0% {
          background-position: 0px;
      }
      100% {
          background-position: -120px;
      }
  }
  .infinite::after {
      content: "";
      position: absolute;
      width: 100%;
      height: 30px;
      background: #333;
      bottom: -30px;
      transform-origin: top;
      transform: perspective(500px) rotateX(-25deg)
  }
  .shadow {
      position: absolute;
      bottom: -93px;
      left: 50%;
      transform: translateX(-50%);
      width: 95%;
      height: 60px;
      background: linear-gradient(#000, transparent);
      opacity: 0.5;
  }

JS:

const path = require('path');
const { readFile } = require('fs');
const app = express();

app.get('/', (request, response) => {
    response.sendFile(path.join(__dirname, "index.html"))
});

app.get('/', (request, responseC) => {
    responseC.sendFile(path.join(__dirname, "style.css"))
});

app.listen(process.env.PORT || 8080, () => console.log('App availible on http://localhost:8080'))

Stackoverflow 告诉我代码太多,文本不够,所以我尝试添加更多。

【问题讨论】:

  • 在 Stack Overflow 上,问题必须是独立的。这意味着问题必须包含所有必要的信息才能回答。
  • 我可以添加什么?抱歉,我在格式化代码时遇到了问题。
  • 请在您的代码中直接提供minimal reproducible example
  • 我将为此进行编辑。希望我能弄清楚。
  • 你有相同的路线。第二个应该是app.get('/style.css', (request, responseC) =&gt; {

标签: javascript html css node.js express


【解决方案1】:

app.get('/', (request, responseC) =&gt; { 更改为app.get('/style.css', (request, responseC) =&gt; {

【讨论】:

    【解决方案2】:

    在您的项目根目录中创建一个名为“public”的文件夹,并使用以下代码在名为 public 的目录中提供图像、CSS 文件和 JavaScript 文件:

    app.use(express.static('public'));
    

    const path = require('path')
    app.use('/static', express.static(path.join(__dirname, 'public')));
    

    如果你做的一切都正确,那么你可以像这样访问文件:

    http://localhost:3000/css/style.css 
    

    css链接

     <link rel="stylesheet" href="/style.css">
    

    【讨论】:

    • 也可以看官方快递文档expressjs.com/en/starter/static-files.html
    • 我已经找到了解决方案。我搞砸了,只是在我的 app.get 中做了“/”。我上面的答案是正确的,我需要再等1天才能标记为正确。
    • 这是在 node.js 中提供静态文件的正确方法。 BlitzDBS 的答案并不常用。
    • 我关注了一个火船视频,我从对原始问题的评论中得到了这个。
    猜你喜欢
    • 2021-07-12
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    相关资源
    最近更新 更多