1

调用函数时出现错误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' });
});

好的。玉:

!!! 5
  html(lang='en')  
    head
      title= title   
    body 
      div#navigation 

包.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');完美。

有什么解决办法吗?

谢谢!

4

1 回答 1

2

最新版本的 Express 似乎使用 app.engine() 而不是 app.set('view engine') ( doc )

app.engine('jade', require('jade').__express);

您可以查看此函数源的更多详细信息。

这可能会解决您的问题。

此外,将“/”添加到您的视图路径(或将其删除,因为它是express的默认值)

编辑:我检查了是否在某处使用了“查看引擎”设置,并且正如本杰明所说,我不明白为什么你的电话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() 而不是已弃用的 '.createServer'
  • /在视图目录路径设置中添加了缺少的内容
  • express.session通话后去掉逗号

待办事项: - 删除文件中的html缩进ok.jade

于 2013-02-19T12:45:31.267 回答