I am having problems using sessions in ExpressJS 4.
I have tried adding session both in server.js (where I set my app's configurations) and in routes.js (where I define my routes). This is what I keep getting:
TypeError: Cannot read property 'connect.sid' of undefined at Layer.session [as handle]
(/Users/larissaleite/Documents/Routing/node_modules/express-session/index.js:115:32) at trim_prefix
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:226:17) at c
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:198:9) at
Function.proto.process_params
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:251:12) at next
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:189:19) at
Layer.expressInit [as handle]
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/middleware/init.js:23:5) at
trim_prefix (/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:226:17)
at c (/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:198:9) at
Function.proto.process_params
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:251:12) at next
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:189:19)
I defined the session like this (server.js):
var express = require('express');
var app = express(); // create app with express
var connect = require('connect');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var passport = require('passport');
var session = require('express-session');
connect()
.use(cookieParser('appsecret'))
.use(function(req, res, next){
res.end(JSON.stringify(req.cookies));
});
app.use(session({ secret: 'appsecret', saveUninitialized: true, cookie: { secure: true, maxAge: new Date(Date.now() + 3600000) }, key:'connect.sid' }));
I've also tried removing the key.
When trying to define the session in routes.js, it was like this:
app.use(session({ secret: 'appsecret', saveUninitialized: true, cookie: { secure: true, maxAge: new Date(Date.now() + 3600000) }, key:'connect.sid' }));
app.use(passport.initialize());
app.use(passport.session());
Remove connect all together. Express 4 was a big overhaul that removed connect as a dependency. remove all parts that require / call connect. Also update all your dependencies.
Per the express-session middleware module's docs:
name - cookie name (formerly known as key). (default: 'connect.sid')
Don't pass it anything and it will use the express sid. I've copied your code and tested it without connect and I'm not getting those errors. Also, I've added the resave
key to the session
config object to mute the deprecated warnings (if you're running the latest express 4 version).
var express = require('express');
var app = express(); // create app with express
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var passport = require('passport');
var session = require('express-session');
app.use(session({
secret: 'appsecret',
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
maxAge: new Date(Date.now() + 3600000)
}
}));
app.listen(1234);