Express Sessions
express-session middleware
Install
Require in server.js
or app.js
Use
Save user information on the session object
For each of the routes you create, the req
variable will now have a session
property which is itself an object. You can assign new properties to this object.
Retrieve user information saved on the session object
Once you add a property to the session object, you can retrieve it when a user navigates to any other route. Then you can use it to make decisions based on the design of your application. Remember though, this session will end when the user closes their browser, or you restart your app.
Destroy the session
You can destroy a session before a user closes their browser window.
Session Store
Out of the box, Express will use MemoryStore
to store session data. This is fine for development and small demo apps. But if you need something meant for production use, you can choose from this list.
If your app uses node-postgres, connect-pg-simple is a good choice.
In server.js
or app.js
Last updated
Was this helpful?