将您的public / css文件集成到ejs中

时间:2019-01-06 17:21:25

标签: node.js express ejs

由于app.css文件不起作用,请帮助我检查代码中是否有错误。

const express = require("express");
const app = express();
const port = process.env.PORT || 4000

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

app.set('views', './views');
app.set('view engine', 'ejs');

app.get("/posts", function(req, res){
var posts = [
    {title: "Once upon a time", author: "Richard"},
    {title: "The Way", author: "Ikoro"},
    {title: "I Love Critics", author: "Emmanuel"},
    {title: "Adorable Bunny", author: "Christian"}
]
res.render("posts", {posts: posts});
});

这是我的公共目录,其中包含app.css文件

body {
background: #ffff00;
color: #800080;
}

1 个答案:

答案 0 :(得分:0)

请westdabestdb查看视图/模板代码

<link rel="stylesheet" href="../public/app.css">
<h1>The Post Page</h1>

<!-- for Loop Syntax for Embedded JavaScript Templates -->
<% for(var i = 0; i < posts.length; i++){ %>
<li>
    <%= posts[i].title %> - <strong><%= posts[i].author %></strong>
</li>
<% } %>

<!-- forEach Loop Syntax for Embedded JavaScript Templates -->
<% posts.forEach(function(post){ %>
<li>
    <%= post.title %> - <strong><%= post.author %></strong>
</li>
<% }) %>