mihai
mihai

Reputation: 38533

How to use cookieSession in express

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?

Upvotes: 10

Views: 8755

Answers (3)

Patrice
Patrice

Reputation: 1444

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

Upvotes: 13

milovanderlinden
milovanderlinden

Reputation: 1194

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.

Upvotes: 1

belltoy
belltoy

Reputation: 76

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.

Upvotes: 2

Related Questions