Question

I'm struggling a bit upgrading to expressjs 4.0. The very simple code below hangs requests to http://localhost:3000/ and no amount of rearranging things fixes that. However, if I comment out the app.use() statements for cookie-parser, body-parser and express-session it works. Obviously I need them so leaving them commented out is not an option.

I know I'm doing something wrong that's very simple but I am not able to see it. Can someone give me a nudge in the right direction?

var express = require('express')
  , cookie = require('cookie-parser')
  , body = require('body-parser')
  , session = require('express-session')
  , http = require('http');

var app = express();
var router = express.Router();

app.use(cookie);
app.use(body);
app.use(session({ secret: 'bigsecret' }));

router.get('/', function(req, res, next) {
  res.send('Welcome');
});

app.use(router);

app.set('port', process.env.PORT || 3000);

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

Solution

The middleware should be passed as function invocations:

app.use(cookie);
app.use(body);

should be

app.use(cookie());
app.use(body());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top