In the last episode, you saw the basics of working with Express sessions – how to configure the session manager and how to store and retrieve information in the session. But at the end of the episode, you also saw that restarting the web server caused all of the session information to be lost! This happens because the default session storage is only in-memory. While this works for small demos, it isn't something that you want to do long-term or in production environments.
In this episode, then, you'll see how to configure MongoDB as a session store using the connect-mongostore module. This module will persist your session information to a MongoDB database, allowing the session information to remain stable when the web server goes down and comes back up.
Along the way, you'll also see a significant problem in using the new session storage – an infinite loop in serializing an object, being masked by multiple rendering errors in the Express application. The path to digging through this is not obvious, but it is an important one to learn. Once the root error is discovered, you'll see how to fix it by only pushing the few bits of information you need in to session storage.