【问题标题】:CSS 404 Node.JSCSS 404 节点.JS
【发布时间】:2017-12-08 03:06:34
【问题描述】:

我已经尝试了很长时间来加载 CSS 文件。我看过其他stackoverflow问题,但没有运气。我有 express.static 语句。我有“标准”文件顺序。看来我在做一些非常明显的错误。

    <html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <nav>
            <ul>
                <li class="inline"><a class="inline" target="_blank" href="http://instagram.com/blakskyben/?hl=en">My Insta</a></li>
                <li class="inline"><a class="inline" href="/about">About Me</a></li>
                <li class="inline"><a class="inline" href="/">Blogs</a></li>
            </ul>
        </nav>
___________________________________________________________________________
    var express = require("express"),
        app = express(),
        bodyParser = require("body-parser"),
        mongoose = require("mongoose");

    mongoose.connect("mongodb://localhost/blog");

    var blogSchema = new mongoose.Schema({
         title: String,
         content: String
    });

    app.use(express.static("public"));
    app.use(express.static("partials"));

    app.use(bodyParser.urlencoded({extended: false}));
    app.use(bodyParser.json());

    app.get("/",function(req,res){
        res.redirect("/blogs");
    });

    app.get("/blogs",function(req,res){
        blog.find({},function(err,foundBlogs){
            if(foundBlogs){
                res.render("blogs.ejs",{blogs: foundBlogs});
            } else {
                res.send("Sorry, an error occured retriving the blogs, " + err);
            }
        });
    });

    app.get("/blogs/:id",function(req,res){
        blog.findById(req.params.id,function(err,foundBlog){
            if(foundBlog){
                res.render("show.ejs",{blog: foundBlog});
            } else {
                res.send("Sorry, but there was an error, " + err);
            }
        });
    });

    app.get("/about",function(req,res){
        res.render("about.ejs");
    });

    var blog = mongoose.model("blog",blogSchema);

    //Seed the DB!

    blog.create({
        title: "Camp",
        content: "Blah." 
    });

    app.listen(8080);

【问题讨论】:

  • style.css 是否存在于您的项目目录中?
  • 它位于公共文件夹内的 CSS 文件夹中。

标签: javascript html css node.js


【解决方案1】:

我认为您的静态目录的路径是错误的。而不是

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

试试

app.use(express.static(__dirname + "/public"));

【讨论】:

    【解决方案2】:

    尝试从应用程序的根目录添加 css 路径,即

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

    或者如果你的 CSS 文件在一个文件夹中,那么

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

    以适合您的为准。

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 2017-03-30
      相关资源
      最近更新 更多