app未在node express中初始化

时间:2014-04-02 05:42:10

标签: node.js express

我刚安装了node和express。当我运行comman node app.js时,它会给我以下错误。

   connect.multipart() will be removed in connect 3.0
   visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
   connect.limit() will be removed in connect 3.0
   /var/www/nodeexpress/nodetest1/app.js:38
   console.log("Express server listening on port %d in %s mode", app.address().
                                                                ^
  TypeError: undefined is not a function
  at Server.<anonymous> (/var/www/nodeexpress/nodetest1/app.js:38:69)
  at Server.g (events.js:196:16)
  at Server.EventEmitter.emit (events.js:101:17)
  at net.js:1159:10
  at process._tickCallback (node.js:339:11)
  at Function.Module.runMain (module.js:492:11)
  at startup (node.js:124:16)
  at node.js:803:3

我的app.js看起来像

 var express = require('express')
  , routes = require('./routes');

//var app = module.exports = express.createServer();
var app = express();

// Configuration

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(8080, function(){
console.log("Express server listening on port %d in %s mode", app.address().port,    app.settings.env);
});

似乎表达没有获得初始化。我的节点版本是v0.11.13-pre,我使用package.json作为3.4.4安装了快速版本

2 个答案:

答案 0 :(得分:3)

使用 this.address()。port 而不是 app.address()。port

address()是应用 listen 函数的http对象的函数属性

来自connect proto.js:

app.listen = function(){
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

对于connect.multipart()警告,请检查以下答案:How to get rid of Connect 3.0 deprecation alert?

答案 1 :(得分:1)

这是解决方案

您需要删除此行,因为bodyParser方法不在此版本中。

app.use(express.bodyParser());

替换为

<强> app.use(express.json());

<强> app.use(express.urlencoded());

如果仍有任何问题,请告诉我。