app.js在快递3中不起作用

时间:2015-09-21 03:36:09

标签: node.js express

我正在

connect deprecated multipart: use parser (multiparty, busboy, formidable) npm module instead at node_modules\express\node_modules\connect\lib\middleware\bodyParser.js
connect deprecated methodOverride: use method-override npm module instead at app.js:22:17
D:\login\node_modules\express\lib\router\index.js:306
    throw new Error(msg);
    ^

Error: .post() requires callback functions but got a [object Undefined]
    at D:\login\node_modules\express\lib\router\index.js:306:11
    at Array.forEach (native)
    at Router.route (D:\login\node_modules\express\lib\router\index.js:302:13)
    at Router.(anonymous function) [as post] (D:\login\node_modules\express\lib\router\index.js:333:16)
    at Function.app.(anonymous function) [as post] (D:\login\node_modules\express\lib\application.js:467:26)
    at Object. (D:\login\app.js:32:5)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

在我的节点中,express.Express版本为3,我无法启动app.js文件。

这是app.js文件:

/**  * Module dependencies.  */

var express = require('express');
var routes = require('./routes');
var signup = require('./routes/login');
var login = require('./routes/signup');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 8000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({secret: 'secretkeyQWERTY'}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')){
  var use=app.use(express.errorHandler());
}

app.get('/', routes.index);
app.post('/signup', signup.signup);
app.post('/login', login.login);


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

1 个答案:

答案 0 :(得分:4)

我刚用快递3.x进行了测试。

您可以通过安装body-parser包轻松解决此问题。不推荐使用express中内置的body-parser连接。

npm install --save body-parser
npm install --save method-override

假设你想接受JSON作为正文...

更改您的代码:

app.use(express.bodyParser())
app.use(express.methodOverride())

为:

var bodyParser = require('body-parser');
var methodOverride = require('method-override')
app.use(bodyParser.json());
app.use(methodOverride());