Registry / database / express-redis

express-redis

JSON →
library2.0.1jsnpmunverified

Express middleware that provides a shared Redis client connection (via the `redis` package) across all requests. Current stable version 2.0.1 uses `redis` ^2.6.2 (peer dependency). Release cadence is low/maintenance only. Key differentiator: simple, zero-config middleware that adds `req.db` for Redis access, with optional custom property name and support for all `redis.createClient` options. Unlike more complex caching or session middleware, it just exposes the raw Redis client per request.

npm install express-redis
INSTALL
IMPORT
SIG · EXPRESS-REDIS
E
express-redis
databasejavascriptv2.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (function)
import redisMiddleware from 'express-redis'
const { redisMiddleware } = require('express-redis')
Package exports a single factory function, not a named export. Default import works in ESM with bundlers. In CommonJS, use require('express-redis')()
require('express-redis')
const middleware = require('express-redis')
const expressRedis = require('express-redis').default
CommonJS: no .default property. The module exports the factory function directly.
req.db
req.db.get('key', callback)
req.redis.get('key', callback)
Default property name is 'db'. To change, pass a custom name as fourth argument: require('express-redis')(port, host, options, 'redis') then use req.redis.

Shows middleware setup with default Redis connection, reading a key via req.db.get().

const express = require('express'); const redisMiddleware = require('express-redis'); const app = express(); // Use middleware with default Redis connection (localhost:6379) app.use(redisMiddleware()); // Example route using Redis app.get('/user/:id', (req, res, next) => { req.db.get(`user:${req.params.id}`, (err, reply) => { if (err) { return next(err); } res.json({ id: req.params.id, data: reply }); }); }); app.listen(3000, () => console.log('Server running on port 3000'));
Debug
Known issues
gotchaThe default property name on req is 'db', not 'redis', which may conflict with other middleware that use 'db'. Use a custom name to avoid collisions.
fix
Specify a custom name like 'redisClient' as the fourth argument: app.use(require('express-redis')(6379, '127.0.0.1', {}, 'redisClient')) then use req.redisClient.
affects: >=0.0.0
deprecatedThe underlying `redis` package v2 is outdated. The `redis` v3+ is a major rewrite with breaking changes (returns promises, uses `createClient` differently). This middleware may not work with newer `redis` versions without compatibility issues.
fix
Either stick with `redis@^2.6.2` as peer dep, or migrate to a middleware that supports `redis@4+` (e.g., implement your own using modern async/await).
affects: >=0.0.0
gotchaThis middleware attaches to `req` only, not `res`. The README example uses `req.db`, but some users mistakenly expect `res.db`.
fix
Use req.db (or custom property name) for Redis access. The custom name string affects both `req` and `res`, but by default only `req.db` exists.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: req.db.get is not a function
Middleware not properly initialized, or called without parentheses: require('express-redis') returns a function, not middleware. You must execute it: require('express-redis')()
fix
Use middleware correctly: app.use(require('express-redis')());
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
Redis server not running or wrong port/host.
fix
Start Redis server (e.g., `redis-server`) or provide correct connection arguments: app.use(require('express-redis')(port, host));
Cannot find module 'redis'
Missing peer dependency `redis`.
fix
Install redis: npm install redis@^2.6.2
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
redisrequiredPeer dependency for Redis client operations
expressoptionalRequired as a peer or installed; middleware expects Express app
Agent activity
7 hits · last 30 days
node
6
Resources
express-redis — npm install express-redis · libregistry