Node.js / Express:错误:无法查找视图“错误”;在views目录中 [英] Nodejs/Express: Error: Failed to lookup view "error" in views directory
问题描述
我将我的nodejs模板引擎切换到了ejs(从jade)。当我使用ejs模板运行app.js时,会收到一系列无法在视图中查找视图'error'日志。
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.
其中一些包括:
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
中查找视图错误是,其中包括许多依赖项在模板本身中(通过脚本标签包含)。使这些内容在快递中正常工作的合适地点是什么?看来express最终不应该在 views
文件夹中查找这些文件(因为它们不是视图)。
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 views
folder (since they aren't views).
推荐答案
请确保您的Express App具有此设置,对于当前的布局,听起来像是您拥有的。
Make sure your Express App has this setup, for the current layout it sounds like you have.
// 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');
对于 res.render( )
放置在应用程序顶层的视图目录中。 express-generator
实际上使用该视图设置。您可以通过修改以下行来更改它
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-generator
actually uses that view setup. You can change that by modifying the below line
// replace with the directory path below ./
app.set('views', path.join(__dirname, 'views'));
这篇关于Node.js / Express:错误:无法查找视图“错误”;在views目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!