Session resets after every app restart in Node/Express
Problem
Whenever I make changes to my app, nodemon restarts the whole app, but every time this happens, my session gets destroyed. This is getting annoying since I have to sign in every time I make changes to my app. How do I avoid this from happening?
I'm using cookie based sessions since I only store the userid. My setup looks like this (in coffeescript):
app.use express.cookieParser()
app.use express.session
secret: 'mysecretkey'
app.use express.csrf()
And I save my session by doing this:
req.session.userid = user._id.toHexString() # it's a mongoDB ObjectID
req.session.save()
Solution
By default expresssJS uses in-memory storage for sessions, so when your app is reset so are the in-memory session.
Since you are using Mongodb I would recommend using mongoDB for your session storage, or redis (which I haven't tried with node).
You can see this question on how to set up session support with express and mongo:
How to store session values with Node.js and mongodb?
Discussion
View additional discussion.