10

I'm trying to use the built in cookieSession object of connect, but I cannot get it to work properly with express.

I have this app:

var express = require('express');
var connect = require('connect');

var app = express.createServer();

app.configure(function() {
  app.use(express.logger());
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('whatever'));
  app.use(connect.cookieSession({ cookie: { maxAge: 60 * 60 * 1000 }}));
});

app.get('/', function(req, res) {
    res.render('root');
});

app.listen(3000);

I'm getting this error:

TypeError: Cannot read property 'connect.sess' of undefined
    at Object.cookieSession [as handle] 

Any ideas?

Chenmunka
  • 685
  • 4
  • 21
  • 25
mihai
  • 37,072
  • 9
  • 60
  • 86

3 Answers3

13

Sessions won't work unless you have these 3 in this order:

app.use(express.cookieParser());
app.use(express.cookieSession());
app.use(app.router);

Working like a charm after that.

see : https://stackoverflow.com/a/10239147/2454820

Community
  • 1
  • 1
Patrice
  • 1,404
  • 1
  • 14
  • 27
2

What is the version of your connect module? The cookieSession middleware was first added in version 2.0.0. Run npm list|grep connect to make sure your connect version is at least 2.0.0 or higher.

belltoy
  • 76
  • 1
  • 4
1

I had this issue too. Turned out that everyauth was amongst the modules linking with connect 1.7.5, after npm remove everyauth, all the issues where gone.

milovanderlinden
  • 1,124
  • 1
  • 12
  • 27