【发布时间】:2021-05-23 05:37:08
【问题描述】:
我在视图目录中收到错误Error: Failed to lookup view "/listings",我尝试将app.set('views', path.join(__dirname, 'views')); 移动到 app.set('view engine', 'ejs');反之亦然没有运气。同样在 app.get 我做了('/listings/index) 仍然一无所获。
我的文件结构
node_modules
views\listings
listings.ejs
index.js
package-lock.json
package.json
index.js
const express = require('express');
const app = express();
const path = require('path');
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
let listings = [{
listingAgent: 'Todd Barkley',
Review: 'Awesome Agent'
},
{
listingAgent: 'David Smith',
Review: 'Good agent'
},
{
listingAgent: 'Erick Jones',
Review: 'Terrible agent, glad we fired him'
}
]
app.get('/listings', (req, res) => {
res.render('/listings', { listings });
})
app.listen(4000, () => {
console.log("On port 4000")
})
index.ejs
<!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>Public Listings</title>
</head>
<body>
<h1>Public Listings</h1>
<ul>
<% for (let l of listings ) {%>
<li>
<%= l.Review %>
<b><%=l.listingAgent %> </b>
</li>
<% } %>
</ul>
</body>
</html>
package.json
{
"name": "practicerest",
"version": "1.0.0",
"description": "Practice ",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Manny Verma",
"license": "ISC",
"dependencies": {
"ejs": "^3.1.6",
"express": "^4.17.1"
}
}
【问题讨论】:
-
你能在问题中添加对你的目录结构的描述吗?
-
我在问题中添加了我的文件结构。
-
所以HTML内容在文件
index.ejs或listings.ejs? -
它在listings.ejs中
-
我的要求好还是需要重新订购?怎么放进去有关系吗?
标签: javascript node.js ejs package.json