1

I am new to nodeJS. I am trying to use different middlewares with connect middleware.

this is my code:

var connect = require('connect');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = connect()
.use(connect.bodyParser())
.use(connect.cookieParser('tobi is a cool ferret'))
.use(function(req, res){
console.log(req.cookies);
console.log(req.signedCookies);
res.end('hello\n');
}).listen(3000);

I have installed every middleware through npm.

I am getting this error while running this file.

 /home/dipesh/Desktop/temp/temp.js:5
 .use(connect.bodyParser())
         ^
  TypeError: Object function createServer() {
   function app(req, res, next){ app.handle(req, res, next); }
    merge(app, proto);
   merge(app, EventEmitter.prototype);
   app.route = '/';
    app.stack = [];
    return app;
  } has no method 'bodyParser'
  at Object.<anonymous> (/home/dipesh/Desktop/temp/temp.js:5:14)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:929:3

Any suggestions?

4

1 回答 1

1
.use(bodyParser())

不是

.use(connect.bodyParser())

您已经 required body-parser,但从未使用过它。

你本质上是在做

var a = function(){};
var b = {};

b.a();

这是不正确的,因为b没有'a'财产。

于 2015-02-26T02:26:34.137 回答