我已经尝试了很长时间来加载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);
答案 0 :(得分:1)
我相信静态目录的路径是错误的。而不是
app.use(express.static("public"));
试
app.use(express.static(__dirname + "/public"));
答案 1 :(得分:0)
尝试从应用程序的root direcotry添加css路径,即
<link rel="stylesheet" href="/style.css">
或者如果你的CSS文件在文件夹中,那么
<link rel="stylesheet" href="/folderName/style.css">
适用于您的任何人。