node.js - 在 ejs 中集成您的 public/css 文件
问题描述
请帮我检查我的代码是否有任何错误,因为 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;
}
解决方案
请 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>
<% }) %>
推荐阅读
- javascript - 没有关于使用sails.js 制作CRUD 的描述
- javascript - 我们不能在 forEach 中重新分配数组值吗?
- macos - MacOS 上的 Chrome 引发“ERR_CERT_WEAK_SIGNATURE_ALGORITHM”警告
- amazon-s3 - 如何删除我的 Hashicorp Vault 中的 400 万份租约
- android - 如何处理“找不到字段的吸气剂”错误?
- python - 如何向下移动第一行的结束图
- postgresql - 从 jsonb 中的列表中提取信息
- android - uses-sdk:minSdkVersion 1 不能小于库中声明的版本 19
- vue.js - 如何在 vue 中将语言环境添加到路由器链接?
- python - 如何在python中解密密码