【问题标题】:css not applying on nodejs even after linking即使在链接后css也没有应用于nodejs
【发布时间】:2021-05-08 18:29:45
【问题描述】:

http://localhost:3000/css/styles.css 给了我:

无法获取 /css/styles.css

.

├── public
├── css
│   ├── styles.css
│─── app.js
│─── signup.html

链接了我的公共静态文件,但是当我运行节点服务器时,css 似乎不适用于我的 html。我附上了在静态与节点端口 3000 上本地运行它的屏幕截图。

希望有人可以帮助我。

我的 app.js

  const express = require("express");
const https = require("https");
const path = require("path");

const app = express();

app.use(express.static("public")); //indicates the directory for static items e.g css

app.get("/", function(req,res){
    res.sendFile(__dirname + "/signup.html");
});



app.listen(3000, function(){
    console.log('server is running on port 3k');
})

我的 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>Sign up for newsletter</title>
    <link href="css/styles.css" rel="stylesheet">
    
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
    <div class="wrapper fadeInDown">
    <div id="formContent">
        <div>
            <h1>Sign up to get the latest updates!</h1>
        </div>
        <!-- Tabs Titles -->

        <!-- Icon -->
        <div class="fadeIn first">
        <img src="https://natisaver.github.io/cv/images/mugicon.png" id="icon" alt="User Icon" />
        </div>

        <!-- Login Form -->
        <form>

【问题讨论】:

  • 如果你在浏览器中打开开发者工具,控制台会显示什么?
  • localhost/:1 拒绝应用来自 'localhost:3000/css/styles.css' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查。跨度>
  • 未捕获的类型错误:无法在 bootstrap.min.js:6 在 bootstrap.min.js:6 localhost/ 的 util.js:10 读取 util.js:68 处未定义的属性“fn”: 1 拒绝应用来自 'localhost:3000/css/styles.css' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查。
  • 我认为这是stylesheet-not-loaded-because-of-mime-type的副本。
  • 在你的目录树结构中它显示_css,这可能是问题吗?

标签: javascript html css node.js express


【解决方案1】:

找到了解决办法,问题是app.js和index.html都在public目录下,把app.js和html文件移出public目录就可以解决问题了。

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 2011-04-11
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多