类型错误:对象函数(req、res、next) {app.handle(req、res、next);}没有方法'configure'。

4
有人能指出为什么我在尝试运行以下代码时会出现此错误吗?
 var express = require('express');
 var login = require('./routes/login');

 var app = express();

 //all environments
 app.configure(function () {
 app.use(express.logger('dev')); 
 app.use(express.bodyParser());
});


app.post('/loginUser',login.loginUser);


app.listen(3000);

console.log("Listening on port 3000...");

我正在使用带有Express 4.x版本的Node.js。

1
Express 4.x版本与早期版本完全不同。因此,所有关于Express的帖子都会导致您走错方向。我建议您现在还是使用较旧的版本。npm install express@3.4.8 - Mukesh Soni
2个回答

10

Tom在他的博客文章new-features-node-express-4中提供了如何从使用express版本3.x的app.configure转换到在express版本4.0中删除它的示例。

为了方便起见,我添加了下面的代码示例。在下面的示例中,您可以用"use"代替"set"。

版本3.x

// all environments
app.configure(function(){
  app.set('title', 'Application Title');
})

// development only
app.configure('development', function(){
  app.set('mongodb_uri', 'mongo://localhost/dev');
})

// production only
app.configure('production', function(){
  app.set('mongodb_uri', 'mongo://localhost/prod');
})

版本 4.0

// all environments
app.set('title', 'Application Title');

// development only
if ('development' == app.get('env')) {
  app.set('mongodb_uri', 'mongo://localhost/dev');
}

// production only
if ('production' == app.get('env')) {
  app.set('mongodb_uri', 'mongo://localhost/prod');
}

8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接