Error: Failed to lookup view "/listings" in views directory
Error: Failed to lookup view "/listings" in views directory
我在 views 目录中收到错误 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"
}
}
首先确保 views
文件夹的名称正确。其次,检查文件扩展名是否正确,最后检查渲染中的路径:
app.get('/listings', (req, res) => {
res.render('listings', { listings }); // remove the slash to get the correct path
})
首先,您的 ejs 文件名为 index
,但您正在尝试渲染 listings
不存在的文件。
其次,您不需要在文件名前使用 /
。
解决方案
所以基本上你所要做的就是将你的 ejs 文件名更改为 listings
或将渲染参数更改为 index
.
最后,删除文件名前的/
。
//index.ejs
app.get('/listings', (req, res) => {
res.render('index', { listings });
})
//listings.ejs
app.get('/listings', (req, res) => {
res.render('listings', { listings });
})
因为文件listings.ejs
在views\listings
文件夹中,代码应该是:
app.get('/listings', (req, res) => {
res.render('listings/listings', { listings });
})
我在 views 目录中收到错误 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"
}
}
首先确保 views
文件夹的名称正确。其次,检查文件扩展名是否正确,最后检查渲染中的路径:
app.get('/listings', (req, res) => {
res.render('listings', { listings }); // remove the slash to get the correct path
})
首先,您的 ejs 文件名为 index
,但您正在尝试渲染 listings
不存在的文件。
其次,您不需要在文件名前使用 /
。
解决方案
所以基本上你所要做的就是将你的 ejs 文件名更改为 listings
或将渲染参数更改为 index
.
最后,删除文件名前的/
。
//index.ejs
app.get('/listings', (req, res) => {
res.render('index', { listings });
})
//listings.ejs
app.get('/listings', (req, res) => {
res.render('listings', { listings });
})
因为文件listings.ejs
在views\listings
文件夹中,代码应该是:
app.get('/listings', (req, res) => {
res.render('listings/listings', { listings });
})