Registry / devops / express-cache-ctrl

express-cache-ctrl

JSON →
library1.2.0jsnpmunverified

Express middleware to manage the Cache-Control header for HTTP caching. Version 1.2.0 is the latest stable release. It provides simple functions like `public()`, `private()`, `disable()`, `secure()`, and `custom()` to set caching directives. Key differentiators include support for ms-formatted TTL strings (e.g., '1d', '2h'), OWASP-recommended secure caching presets, and fine-grained control via an options object. Compared to other caching middleware like `helmet` or `express-cache-headers`, it focuses solely on Cache-Control and is lightweight. Requires Node >= 18.20.8.

npm install express-cache-ctrl
INSTALL
IMPORT
SIG · EXPRESS-CACHE-CTRL
E
express-cache-ctrl
devopsjavascriptv1.2.0
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.

cache
const cache = require('express-cache-ctrl')
import cache from 'express-cache-ctrl'
Package is CommonJS-only; no ESM support. Use require().
cache.disable
app.use('/api', cache.disable())
app.use('/api', cache.disable)
disable() returns middleware; call it as a function.
cache.public
app.use('/static', cache.public('1d'))
app.use('/static', cache.public(86400))
Accepts number seconds or string like '1d'; both work.

Creates an Express app, disables caching for /api, sets public cache with 1-day TTL for /static, and starts the server.

const express = require('express'); const cache = require('express-cache-ctrl'); const app = express(); // Disable caching for API routes app.use('/api', cache.disable()); // Set public caching for static assets app.use('/static', cache.public('1d')); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000);
Debug
Known issues
breakingRequires Node.js >= 18.20.8. Older versions will fail with a syntax error or missing features.
fix
Upgrade Node.js to 18.20.8 or later.
affects: <18.20.8
gotchaThe callback for disable() must be invoked (disable()) not passed as a reference (disable). Failing to call it results in no cache headers.
fix
Always call the function: app.use('/api', cache.disable())
affects: >=0.0.0
gotchaTTL string parsing uses the `ms` library; invalid strings will throw an error. For example, '1x' is not valid.
fix
Use valid ms format: numbers, '1s', '1m', '1h', '1d', '1w', '1y'. Avoid unsupported strings.
affects: >=0.0.0
deprecatedThe `noCache` option in the options object is deprecated; use `cache.noCache()` instead.
fix
Replace `{ noCache: true }` with `cache.noCache()` or use `cache.disable()`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cache.disable is not a function
Forgetting to call require() or using wrong import style.
fix
Use `const cache = require('express-cache-ctrl');` instead of `import cache from 'express-cache-ctrl';`
RangeError: ttl must be a number or a valid ms string
Passing an invalid TTL string (e.g., '1x') to cache.public() or cache.private().
fix
Use a valid string like '1d', '2h', or a number in seconds.
TypeError: app.use() requires a middleware function
Passing `cache.disable` (function reference) instead of `cache.disable()` (function call).
fix
Call the function: `app.use('/api', cache.disable())`
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
msrequiredUsed to parse TTL strings like '1d' into seconds.
Agent activity
2 hits · last 30 days
node
2
Resources
express-cache-ctrl — npm install express-cache-ctrl · libregistry