node.js 错误:无法在 Express 中查找视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10216395/
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
Error: Failed to lookup view in Express
提问by nax
Note: my auto answer at end of the post
注意:我在帖子末尾的自动回答
I'm trying to make a better experience of nodeJS and i don't really like to get all the script in one file.
我正在尝试获得更好的 nodeJS 体验,但我真的不喜欢将所有脚本都放在一个文件中。
so, following a post here i use this structure
所以,按照这里的帖子,我使用这个结构
./
config/
enviroment.js
routes.js
public/
css/
styles.css
images
views
index
index.jade
section
index.jade
layout.jade
app.js
My files are right now:
我的文件现在是:
app.js
应用程序.js
var express = require('express');
var app = module.exports = express.createServer();
require('./config/enviroment.js')(app, express);
require('./config/routes.js')(app);
app.listen(3000);
enviroment.js
环境.js
module.exports = function(app, express) {
app.configure(function() {
app.use(express.logger());
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade'); //extension of views
});
//development configuration
app.configure('development', function() {
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
//production configuration
app.configure('production', function() {
app.use(express.errorHandler());
});
};
routes.js
路由.js
module.exports = function(app) {
app.get(['/','/index', '/inicio'], function(req, res) {
res.render('index/index');
});
app.get('/test', function(req, res) {
//res.render('index/index');
});
};
layout.jade
layout.jade
!!! 5
html
head
link(rel='stylesheet', href='/css/style.css')
title Express + Jade
body
#main
h1 Content goes here
#container!= body
index/index.jade
索引/索引.jade
h1 algoa
The error i get is:
我得到的错误是:
Error: Failed to lookup view "index/index" at Function.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\application.js:495:17) at render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:614:9) at ServerResponse.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:638:5) at c:\xampp\htdocs\nodejs\buses\config\routes.js:4:7 at callbacks (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:177:11) at param (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:151:11) at pass (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:158:5) at Router._dispatch (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:185:4) at Object.router [as handle] (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:45:10) at next (c:\xampp\htdocs\nodejs\buses\node_modules\express\node_modules\connect\lib\proto.js:191:15)
错误:在渲染时无法在 Function.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\application.js:495:17) 处查找视图“索引/索引” (c:\xampp\htdocs) \nodejs\buses\node_modules\express\lib\response.js:614:9) 在 ServerResponse.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:638:5) 在c:\xampp\htdocs\nodejs\buses\config\routes.js:4:7 在回调 (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:177:11)在参数 (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:151:11) 处 (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\ router\index.js:158:5) 在 Router._dispatch (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:185:4) 在 Object.router [作为句柄] (C:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:45:10) 在下一个 (c:\xampp\htdocs\nodejs\buses\node_modules\express\node_modules\connect\lib\proto .js:191:15)
But i don't really know what is the problem...
但我真的不知道是什么问题...
I'm starting thinking is because the modules exports...
我开始思考是因为模块导出...
Answer:Far away the unique solution i found is to change the place i defined app.set('views') and views engine
答:我发现的唯一解决方案是更改我定义的地方 app.set('views') 和视图引擎
I moved it to the app.js and now is working well.
我将其移至 app.js,现在运行良好。
var express = require('express');
var app = module.exports = express.createServer();
require('./config/enviroment.js')(app, express);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
require('./config/routes.js')(app);
app.listen(3000);
I don't really understand the logic behind this but i gonna supose it have one.
我真的不明白这背后的逻辑,但我会假设它有一个。
采纳答案by mihai
npm install [email protected]installs the previous version, if it helps.
npm install [email protected]如果有帮助,安装以前的版本。
I know in 3.x the view layout mechanic was removed, but this might not be your problem. Also replace express.createServer()with express()
我知道在 3.x 中删除了视图布局机制,但这可能不是您的问题。也替换express.createServer()为express()
Update:
更新:
It's your __dirname from environment.js
It should be:
这是您来自 environment.js 的 __dirname
它应该是:
app.use(express.static(__dirname + '../public'));
回答by Veera
Adding to @mihai's answer:
添加到@mihai 的回答中:
If you are in Windows, then just concatenating __dirname' + '../public'will result in wrong directory name (For example: c:\dev\app\module../public).
如果您使用的是 Windows,那么仅连接__dirname' + '../public'将导致错误的目录名称(例如:)c:\dev\app\module../public。
Instead use path, which will work irrespective of the OS:
而是使用path,无论操作系统如何,它都可以工作:
var path = require ('path');
app.use(express.static(path.join(__dirname + '../public')));
path.joinwill normalize the path separator character and will return correct path value.
path.join将规范化路径分隔符并返回正确的路径值。
回答by Ahsan Ullah Sarbaz
I had the same error at first and i was really annoyed.
you just need to have ./before the path to the template
起初我有同样的错误,我真的很生气。你只需要./在模板路径之前
res.render('./index/index');
Hope it works, worked for me.
希望它有效,对我有用。
回答by Codemaker
It is solved by adding the following code in app.js file
在app.js文件中添加如下代码解决
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname);
app.get('/', function(req, res){
res.render("index");
});
回答by Harish_Madugula
You could set the path to a constant like this and set it using express.
您可以将路径设置为这样的常量并使用 express 进行设置。
const viewsPath = path.join(__dirname, '../views')
app.set('view engine','hbs')
app.set('views', viewsPath)
app.get('/', function(req, res){
res.render("index");
});
This worked for me
这对我有用
回答by nilakantha singh deo
Check if you have used a proper view engine. In my case I updated the npm and end up in changing the engine to 'hjs'(I was trying to uninstall jade to use pug). So changing it to jade from hjs in app.js file worked for me.
检查您是否使用了正确的视图引擎。就我而言,我更新了 npm 并最终将引擎更改为“hjs”(我试图卸载 jade 以使用 pug)。因此,将它从 app.js 文件中的 hjs 更改为 jade 对我有用。
app.set('view engine','jade');
回答by user9160528
This problem is basically seen because of case sensitive file name. for example if you save file as index.jadge than its mane on route it should be "index" not "Index" in windows this is okay but in linux like server this will create issue.
这个问题基本上是因为区分大小写的文件名。例如,如果你将文件保存为 index.jadge 而不是它在路由上的鬃毛,它应该是“索引”而不是“索引”在 Windows 中这是可以的,但在像服务器这样的 linux 中这会产生问题。
1)if file name is index.jadge
1)如果文件名是index.jadge
app.get('/', function(req, res){
res.render("index");
});
2)if file name is Index.jadge
2)如果文件名是Index.jadge
app.get('/', function(req, res){
res.render("Index");
});
回答by KARTHIKEYAN.A
use this code to solve the issue
使用此代码解决问题
app.get('/', function(req, res){
res.render("index");
});
回答by Bobby
Just noticed that I had named my file ' index.html'instead for 'index.html'with a leading space. That was why it could not find it.
只注意到我已经叫我的文件,' index.html'而不是为'index.html'一个领先的空间。这就是它找不到它的原因。
回答by Hilary Mwape
This error really just has to do with the file Path,thats all you have to check,for me my parent folder was "Layouts" but my actual file was layout.html,my path had layouts on both,once i corrected that error was gone.
这个错误实际上只与文件路径有关,这就是您需要检查的全部内容,对我来说,我的父文件夹是“布局”,但我的实际文件是layout.html,我的路径在两个文件上都有布局,一旦我更正该错误就消失了。

