调用render
函数时出现错误[此应用程序在Heroku上运行]
web.js:
var async = require('async');
var express = require('express');
var util = require('util');
// create an express webserver
var app = express.createServer(
express.logger(),
express.static(__dirname + '/public'),
express.bodyParser(),
express.cookieParser(),
// set this to a secret value to encrypt session cookies
express.session({ secret: process.env.SESSION_SECRET || 'secret123' }),
);
app.set('views', __dirname + 'views');
app.set('view engine', 'jade');
// listen to the PORT given to us in the environment
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});
app.get('/', function ( req, res ) {
res.render('ok', { title : 'ok' });
});
ok.jade:
!!! 5
html(lang='en')
head
title= title
body
div#navigation
的package.json:
{
"name": "package",
"version": "0.0.1",
"description": "Facebook integration, Node.js",
"dependencies": {
"express": "latest",
"jade":"0.17.0"
},
"engines": {
"node": "0.8.19",
"npm" : "1.2.10"
}
}
错误:
TypeError: Cannot read property '0' of undefined
at new View (/app/node_modules/express/lib/view.js:41:65)
at Function.app.render (/app/node_modules/express/lib/application.js:486:12)
at ServerResponse.res.render (/app/node_modules/express/lib/response.js:753:7)
at /app/web.js:32:9
at callbacks (/app/node_modules/express/lib/router/index.js:161:37)
at param (/app/node_modules/express/lib/router/index.js:135:11)
at pass (/app/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/app/node_modules/express/lib/router/index.js:170:5)
at Object.router (/app/node_modules/express/lib/router/index.js:33:10)
at next (/app/node_modules/express/node_modules/connect/lib/proto.js:199:15)
如果我将res.render('ok', {title : 'ok'});
替换为res.end('some text');
完美无缺。
任何解决方案?
谢谢!
答案 0 :(得分:2)
Express的最新版本似乎使用app.engine()而不是app.set('view engine')(doc)
app.engine('jade', require('jade').__express);
您可以查看更多详情this function source。
这可以解决您的问题。
另外,在您的观看路径中添加“/”(或将其移除为快递中的it's default)
编辑:我检查了“查看引擎”设置是否在某处使用it is,并且像本杰明说的那样,我不明白为什么你对app.set('view engine')
的调用不起作用。
编辑2:我只是试图运行你的代码,但有几个失败点。这是一个固定的web.js文件,可以在我的本地计算机上运行:
var express = require('express');
var util = require('util');
// create an express webserver
var app = express(
express.logger(),
express.static(__dirname + '/public'),
express.bodyParser(),
express.cookieParser(),
// set this to a secret value to encrypt session cookies
express.session({ secret: process.env.SESSION_SECRET || 'secret123' })
);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// listen to the PORT given to us in the environment
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});
app.get('/', function ( req, res ) {
res.render('ok', { title : 'ok' });
});
更新日志:
require('async')
,因为它未被使用,而不是在package.json /
express.session
致电 Todo在你身边:
- 删除html
文件中ok.jade
之前的缩进。