How to use cookieSession in express
Asked Answered
G

3

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?

Gasparo answered 29/3, 2012 at 12:55 Comment(2)
Any special reason you're using connect.cookieSession instead of express.cookieSession?Fiddlestick
@AndersBornholm the question is pretty old, it was probably not available at that pointGasparo
N
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.

Narcisanarcissism answered 29/3, 2012 at 14:43 Comment(2)
Express comes with connect 1.8.6, so yeah, that's probably why it's not working. ThanksGasparo
Express 3 comes with connect 2.7.Perdurable
S
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://mcmap.net/q/593463/-39-session-39-is-undefined-when-using-express-redis-for-session-store

Suzette answered 17/6, 2013 at 7:0 Comment(3)
I'm getting Error: secret option required for cookie sessionsHakenkreuz
Just add a secret: app.use(express.cookieSession({secret:'mysecret'}));Alla
Remove the app.use(app.router) and it worked for me. (also had to had to add the secret)Pangenesis
N
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.

Narcisanarcissism answered 29/3, 2012 at 14:43 Comment(2)
Express comes with connect 1.8.6, so yeah, that's probably why it's not working. ThanksGasparo
Express 3 comes with connect 2.7.Perdurable
P
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.

Pronty answered 3/5, 2013 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.