This tutorial shows how to use Serverless Redis as your session storage for your Express Applications.
See the code
Create a folder for your project and run:
In your project folder run:
npm install express redis connect-redis express-session
Create a database as described here.
In Upstash console, click the
Connect button, copy the connection code (Node.js node-redis).
Create index.js file as below and replace the Redis connection part.
Open http://localhost:3000/bar and http://localhost:3000/foo in different browsers. Check if the view-count is incrementing as expected.
There is a default session storage of express-session. Why do I need Redis?
Default session store loses the session data when the process crashes. Moreover, it does not scale. You can not utilize multiple web servers to serve your sessions.
You can use any Redis offering or self hosted one. But Upstash's serverless approach with per-request-pricing will help you to minimize your cost with zero maintenance.
How to configure the session storage?