1

I have noticed that in order to store a value into the session you simply call req.session.key = value without the need to specify a callback. I have set mysql as my session storage adapter using the connect-mysql module. So I am wondering that consider each time I save a value to the session it is being updated in the db, shouldn't there be a callback associated with this? Yet everywhere I look people are happily using it synchronously. Can someone please explain why this is the case? THanks.

0

1 Answer 1

1

The session middleware only actually interacts with the data-store twice per request, rather than immediately with each change:

  1. With Store#get() to retrieve the Session in bulk at the start of a request. (source)
  2. With Store#set() (via Session#save()) to persist the Session in bulk at the end of the request. (source)

Between these steps, changes to the session can be done synchronously. They just should be done before res.end() or similar (res.render(), res.json(), etc.) is called.

1
  • Thanks that makes perfect sense.
    – ReneGAED
    Jul 29, 2012 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.