question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Express creating new session for each request

See original GitHub issue

Express-session is creating a new session (new sessionID) for new request. And it happens intermittently, for example sometimes for first 3-4 hits it will work fine and keep using same existing session and on 4th hit it will create a new one, Sometimes it will create new session on first request itself.

Things that i have already tried and are not working…

Express session is created after static file routes. Express session secret and cookieParser secret are set to same. Using app.use(favicon(path.join(__dirname, ‘public’, ‘favicon.ico’))); for favicon. Setting very high value for max-age field while creating cookie. Below is my app.js snippet -

var passport = require('passport');
var expressSession = require('express-session');
var app = express();
app.engine('hbs', hbs.express4({
  partialsDir: __dirname + '/views/partials'
}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(express.static(path.join(__dirname, 'public')));
var flash = require('connect-flash');
app.use(flash());

var initPassport = require('./passport/init');
initPassport(passport);
//app.use(bodyParser.urlencoded());
app.use(cookieParser('mysecret'));
app.use(expressSession({ secret: 'mysecret', name:'user', cookie: {maxAge: 60000000}}));
app.use(passport.initialize());
app.use(passport.session());

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
GeniaTcommented, Mar 31, 2021

For those googling around for this same issue, I fixed mine by setting the flag saveUninitialized: false in the session middleware.

4reactions
PaulBGDcommented, Jul 22, 2016

I’ve encountered a similar problem, except it was because I wasn’t including cookies when using the browser fetch.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Express-session creates new session every request
Turns out all I was missing was app.set('trust proxy', 1) in my setup and sameSite: 'none' in cookie: {} in session middleware.
Read more >
Express session middleware
To store or access session data, simply use the request property req.session , which is (generally) serialized as JSON by the store, so...
Read more >
Session Management in Node.js using ExpressJS and ...
Express -session - an HTTP server-side framework used to create and manage a session middleware. This tutorial is all about sessions.
Read more >
How to Manage Session using Node.js and Express
Create a new folder and switch to it using the terminal. Run this command to create ... Now using 'request' variable you can...
Read more >
Cookie and Session (II): How session works in express-session
Check if this request has cookie with valid session ID · If yes, search for the matching session ID in session store and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found