javascript TypeError: Object function (req, res, next) { app.handle(req, res, next); 没有方法“配置”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22265143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:48:30  来源:igfitidea点击:

TypeError: Object function (req, res, next) { app.handle(req, res, next); } has no method 'configure'

javascriptnode.jsexpress

提问by msrameshp

Can anyone point out why am I getting this error when I am trying to run the following code ?

谁能指出为什么我在尝试运行以下代码时会收到此错误?

 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...");

I am using node.js with the express 4.x version.

我正在使用带有 express 4.x 版本的 node.js。

回答by Mike Barlow - BarDev

Tom in his blog post new-features-node-express-4provides examples of how to convert from using app.configure in express version 3.x to removing it in express version 4.0.

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

For convenience I added the code example below. In the examples below you can replace "set" with "use".

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

Version 3.x

版本 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');
})

Version 4.0

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');
}

回答by alex

Express 4.x does not have configure method.

Express 4.x 没有配置方法。

https://github.com/visionmedia/express/wiki/Migrating-from-3.x-to-4.x

https://github.com/visionmedia/express/wiki/Migrating-from-3.x-to-4.x

Also, it doesn't have express.loggerand express.bodyParserhad been deprecated ages ago.

此外,它在很久以前没有express.logger并且express.bodyParser已经被弃用。