Registry / web-framework / cacheable-middleware

cacheable-middleware

JSON →
library1.0.0jsnpmunverified

Middleware for Express/Connect to set HTTP cache headers (Cache-Control, Expires) on responses. Version 1.0.0 (latest) requires Node >=6. Methods: res.cacheFor(milliseconds) or res.cacheFor(amount, unit) using Moment.js duration syntax. Can also set default cache duration for all routes in a middleware stack. Lightweight, no dependencies beyond Node built-ins. Differs from express-cache-headers by offering both per-response and global configuration.

npm install cacheable-middleware
INSTALL
IMPORT
SIG · CACHEABLE-MIDDLEWA
C
cacheable-middleware
web-frameworkjavascriptv1.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

default function
✓ import cacheable from 'cacheable-middleware';
✗ const { cacheable } = require('cacheable-middleware');
Default export is the middleware factory function. Named export does not exist.
default (CommonJS)
✓ const cacheable = require('cacheable-middleware');
✗ const { cacheable } = require('cacheable-middleware');
CommonJS require returns the default export as a function, not an object with named properties.
TypeScript types
✓ import cacheable from 'cacheable-middleware';
No official TypeScript types. Use `@types/cacheable-middleware` or declare module manually.

Demonstrates two usage patterns: global cache duration applied to a path and per-response control via res.cacheFor().

const express = require('express'); const cacheable = require('cacheable-middleware'); const app = express(); // Set default cache to 1 hour (3600000 ms) for all /static routes app.use('/static', cacheable(3600000)); // Per-route caching using res.cacheFor() app.get('/dynamic', (req, res) => { res.cacheFor(60000); // cache for 1 minute res.json({ message: 'this response is cached for 1 minute' }); }); // Use Moment.js duration syntax (value, unit string) app.get('/long', (req, res) => { res.cacheFor(6, 'months'); res.send('cached for 6 months'); }); app.listen(3000);
Debug
Known issues
breakingVersion 1.0.0 drops support for Node.js < 6. Requires ES6 features.
fix
Upgrade Node.js to version 6 or higher.
affects: >=1.0.0
deprecatedEquivalent functionality exists in HTTP framework built-in features (Express 'static' middleware cache options) and dedicated cache-control modules.
fix
Consider using express.static with maxAge option for static files: app.use('/static', express.static('public', { maxAge: '1d' })).
affects: >=1.0.0
gotchaDuration units are parsed by Moment.js; invalid strings may throw or produce unexpected durations.
fix
Use valid Moment.js duration formats: number (milliseconds) or number + string (e.g., 6, 'months', 3, 'hours').
affects: >=1.0.0
gotchares.cacheFor() must be called before res.send() or res.json() to set headers properly.
fix
Call res.cacheFor() at the beginning of the route handler, before sending response.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: cacheable is not a function
Importing incorrectly (e.g., named import when using default export).
fix
Use default import: const cacheable = require('cacheable-middleware');
Error: Cannot find module 'cacheable-middleware'
Package not installed or typo in package name.
fix
Run npm install cacheable-middleware and verify package.json.
TypeError: res.cacheFor is not a function
Middleware not applied before route handler.
fix
Ensure app.use(cacheable()) is called before defining routes that use res.cacheFor().
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
24 hits · last 30 days
node
22
Amazon
1
OpenAI (training)
1
Resources
cacheable-middleware — npm install cacheable-middleware · libregistry