0

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()
Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118

1 Answers1

5

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?

Community
  • 1
  • 1
Craig MacGregor
  • 4,589
  • 1
  • 21
  • 13
  • aw crap. why isn't that said in the guide? i assumed it was cookie based. thanks! – Jonathan Ong Mar 30 '12 at 04:11
  • which mongodb-based session should i use? https://github.com/kcbanner/connect-mongo or https://github.com/masylum/connect-mongodb? i can't tell a difference. – Jonathan Ong Mar 30 '12 at 04:14
  • I'm using connect-mongo and have had no problems. It's fairly straightforward to plug-in the other if you encounter something you don't like about one of them :) – Craig MacGregor Mar 30 '12 at 04:22