Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Session resets after every app restart in Node/Express

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()
Problem courtesy of: Jonathan Ong

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?

Solution courtesy of: Craig MacGregor

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Session resets after every app restart in Node/Express

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×