错误:无法在 Express 中查找视图 [英] Error: Failed to lookup view in Express
问题描述
注意:我在帖子末尾自动回复
Note: my auto answer at end of the post
我正在努力改善 nodeJS 的体验,但我真的不喜欢将所有脚本都放在一个文件中.
I'm trying to make a better experience of nodeJS and i don't really like to get all the script in one file.
所以,按照这里的帖子,我使用这种结构
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
我的文件现在是:
app.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
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
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
!!! 5
html
head
link(rel='stylesheet', href='/css/style.css')
title Express + Jade
body
#main
h1 Content goes here
#container!= body
index/index.jade
h1 algoa
我得到的错误是:
错误:无法查找视图索引/索引"在 Function.render (c:xampphtdocs odejsuses ode_modulesexpresslibapplication.js:495:17)在渲染 (c:xampphtdocs odejsuses ode_modulesexpresslib esponse.js:614:9)在 ServerResponse.render (c:xampphtdocs odejsuses ode_modulesexpresslib esponse.js:638:5)在 c:xampphtdocs odejsusesconfig outes.js:4:7在回调 (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:177:11)在参数 (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:151:11)通过 (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:158:5)在 Router._dispatch (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:185:4)在 Object.router [作为句柄] (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:45:10)接下来 (c:xampphtdocs odejsuses ode_modulesexpress ode_modulesconnectlibproto.js:191:15)
Error: Failed to lookup view "index/index" at Function.render (c:xampphtdocs odejsuses ode_modulesexpresslibapplication.js:495:17) at render (c:xampphtdocs odejsuses ode_modulesexpresslib esponse.js:614:9) at ServerResponse.render (c:xampphtdocs odejsuses ode_modulesexpresslib esponse.js:638:5) at c:xampphtdocs odejsusesconfig outes.js:4:7 at callbacks (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:177:11) at param (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:151:11) at pass (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:158:5) at Router._dispatch (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:185:4) at Object.router [as handle] (c:xampphtdocs odejsuses ode_modulesexpresslib outerindex.js:45:10) at next (c:xampphtdocs odejsuses ode_modulesexpress ode_modulesconnectlibproto.js:191:15)
但我真的不知道是什么问题...
But i don't really know what is the problem...
我开始思考是因为模块导出...
I'm starting thinking is because the modules exports...
答案:我发现的唯一解决方案是更改我定义的地方 app.set('views') 和视图引擎
Answer: Far away the unique solution i found is to change the place i defined app.set('views') and views engine
我将其移至 app.js,现在运行良好.
I moved it to the app.js and now is working well.
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.
推荐答案
npm install express@2.5.9
安装以前的版本,如果有帮助的话.
npm install express@2.5.9
installs the previous version, if it helps.
我知道在 3.x 中删除了视图布局机制,但这可能不是您的问题.同时将 express.createServer()
替换为 express()
I know in 3.x the view layout mechanic was removed, but this might not be your problem. Also replace express.createServer()
with express()
这是你的 __dirname 来自 environment.js
应该是:
It's your __dirname from environment.js
It should be:
app.use(express.static(__dirname + '../public'));
这篇关于错误:无法在 Express 中查找视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!