![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
alt-session
Advanced tools
This module provides your apps with asynchronous Session API with data stored in Redis.
The asynchronous approach allows you to access session data on-demand, instead of saving-restoring it on every request regardless of whether data is actually being used or not. It is also super-friendly to asynchronous control flow libraries (like async).
npm install alt-session
Add middleware after cookie parser:
app.use(require('alt-session', {
redis: {
host: 'localhost',
port: 6390,
auth_pass: 'optional'
},
session: {
dbIndex: 0, // for selecting Redis database
tti: 300, // time to idle before session is removed from Redis, in seconds
prefix: 'sess', // custom key prefix for Redis storage
secure: true, // for setting cookie.secure option
domain: 'optional' // for custom cookie domain
}
}));
Store session data:
req.session.set('myKey', 'myValue', function(err) {
if (err)
return next(err);
// Success
});
Retrieve single key:
req.session.get('myKey', function(err, myValue) {
if (err)
return next(err);
// Success
});
Retrieve multiple keys:
req.session.mget(['myKey1', 'myKey2'], function(err, session) {
if (err)
return next(err);
// Success
// session.myKey1
// session.myKey2
});
Remove single value:
req.session.remove('myKey', function(err) {
if (err)
return next(err);
// Success
});
Remove all values:
req.session.remove(['myKey1', 'myKey2'], function(err) {
if (err)
return next(err);
// Success
});
Invalidate (clear):
req.session.invalidate(function(err) {
if (err)
return next(err);
// Success
});
Asynchronous Session API is generally incompatible with synchronous version.
If your library depends on certain keys being available in req.session
, you might want to add simple middleware like this:
// Retrieve stuff
app.use(function(req, res, next) {
req.session.get('myKey', function(value) {
req.session.myKey = value;
next();
});
});
// Include your other middleware and routes
// Persist stuff
app.use(function(req, res, next) {
req.session.set('myKey', req.session.myKey, next);
});
FAQs
Alternative API for Express sessions (backed by Redis)
The npm package alt-session receives a total of 5 weekly downloads. As such, alt-session popularity was classified as not popular.
We found that alt-session demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.