Javascript Nodejs/Express:错误:无法在视图目录中查找视图“错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35604730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:57:40  来源:igfitidea点击:

Nodejs/Express: Error: Failed to lookup view "error" in views directory

javascriptnode.jsexpressejs

提问by ApathyBear

I switched my nodejs template engine over to ejs (from jade). When I run my app.js with my ejs template, I receive a series of "Failed to lookup view 'error' in views" logs.

我将我的 nodejs 模板引擎切换到 ejs(来自 jade)。当我使用 ejs 模板运行 app.js 时,我收到一系列“无法在视图中查找视图‘错误’”日志。

Some of which include:

其中一些包括:

GET /css/bootstrap.min.css 500 12.588 ms - 1390
Error: Failed to lookup view "error" in views directory
...
GET /css/clean-blog.min.css
Error: Failed to lookup view "error" in views directory
...
GET /js/bootstrap.min.js
Error: Failed to lookup view "error" in views directory
...
GET /js/jquery.js
Error: Failed to lookup view "error" in views directory

Thing is, many of these dependencies are included in the template itself (included via script tags). What is the proper place to get these to work in express? It seems like express ultimately should not be looking for these in the viewsfolder (since they aren't views).

事实是,这些依赖项中的许多都包含在模板本身中(通过脚本标签包含)。让这些在快递中工作的正确位置是什么?似乎 express 最终不应该在views文件夹中查找这些(因为它们不是视图)。

回答by peteb

Make sure your Express App has this setup, for the current layout it sounds like you have.

确保您的 Express App 具有此设置,对于当前布局,它听起来像是您拥有的。

// Require static assets from public folder
app.use(express.static(path.join(__dirname, 'public')));

// Set 'views' directory for any views 
// being rendered res.render()
app.set('views', path.join(__dirname, 'views'));

// Set view engine as EJS
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

It is pretty normal for views that are getting rendered by res.render()to be placed in a 'Views' directory at the top level of your app. The express-generatoractually uses that view setup. You can change that by modifying the below line

将被渲染res.render()的视图放置在应用程序顶层的“视图”目录中是很正常的。在express-generator实际使用该视图设置。您可以通过修改以下行来更改它

// replace with the directory path below ./
app.set('views', path.join(__dirname, 'views'));

回答by Plaute

It seems that Express doesn't find your files, so your poor little server want to return an error, but your error file is missing in viewsdirectory.

Express 似乎没有找到你的文件,所以你可怜的小服务器想要返回一个错误,但是你的错误文件在views目录中丢失了。

In views directory, you have just to create a file called error.jade.

在 views 目录中,您只需创建一个名为 error.jade 的文件。

Then you have to search where Express searches your files yet.

然后你必须搜索 Express 搜索文件的位置。

回答by Nagnath Mungade

I resolved my issue by using below code.

我使用以下代码解决了我的问题。

    // Require static assets from public folder
app.use(express.static(path.join(__dirname, 'public')));
// Set view engine as EJS
app.engine('ejs', require('ejs').renderFile);
app.set('view engine', 'ejs');
// Set 'views' directory for any views 
// being rendered res.render()
app.set('views', path.join(__dirname, ''));
app.use('/form', express.static(__dirname + '/index.html'));