Express Cache Controller Middleware
raw JSON →express-cache-controller is an Express.js middleware designed to simplify the management of Cache-Control HTTP headers for client-side caching. It offers a straightforward API to configure directives such as `max-age`, `no-cache`, `no-store`, `public`, and `private` globally for an application or on a per-route basis via `res.cacheControl`. The package is lightweight and focuses solely on setting response headers to guide browser and proxy caching behavior. The current stable and only version, 1.1.0, was last published in October 2017, indicating it is effectively an abandoned project with no ongoing development or official support. Users integrating this package into modern Node.js environments should be aware of its CommonJS-only nature and the absence of recent security patches or feature enhancements compared to more actively maintained caching solutions.
Common errors
error TypeError: require(...) is not a function ↓
const cacheControl = require('express-cache-controller'); app.use(cacheControl(options)); error Cache-Control header is missing or incorrect in response. ↓
app.use(cacheControl()) is called early in your Express app. Verify res.cacheControl = { ... } is used correctly for overrides. Inspect network requests to confirm the header is being sent as expected and debug for conflicting middleware. error ReferenceError: cacheControl is not defined ↓
const cacheControl = require('express-cache-controller'); at the top of your file. If you must use ESM, consider wrapping it or finding a more modern alternative. Warnings
breaking The package is CommonJS-only. Attempting to use `import` syntax in an ESM module without proper transpilation or configuration will result in errors. ↓
gotcha This package is effectively abandoned. It has not been updated since October 2017 (v1.1.0), meaning there will be no further bug fixes, security patches, or feature enhancements. ↓
gotcha Using this middleware does not implement server-side caching. It only sets the `Cache-Control` HTTP response header, instructing clients (browsers, proxies) on how to cache content. It will not reduce server load from actual request processing. ↓
gotcha Incorrect middleware order can lead to `Cache-Control` headers being overridden by other middleware or explicit `res.set()` calls later in the request-response cycle. ↓
Install
npm install express-cache-controller yarn add express-cache-controller pnpm add express-cache-controller Imports
- cacheControl wrong
import cacheControl from 'express-cache-controller';correctconst cacheControl = require('express-cache-controller'); - cacheControl wrong
app.use(require('express-cache-controller')());correctapp.use(cacheControl({ maxAge: 3600 })); - res.cacheControl
res.cacheControl = { maxAge: 30, public: true };
Quickstart
const express = require('express');
const cacheControl = require('express-cache-controller');
const app = express();
const PORT = process.env.PORT || 3000;
// Apply cache control middleware with a default maxAge of 60 seconds
app.use(cacheControl({ maxAge: 60 }));
// Route with default caching (max-age=60)
app.get('/', (req, res) => {
res.send('This page will be cached for 60 seconds.');
});
// Route overriding the default cache control to be shorter (30 seconds)
app.get('/short-cache', (req, res) => {
res.cacheControl = {
maxAge: 30
};
res.send('This page will be cached for 30 seconds.');
});
// Route setting no-cache
app.get('/no-cache', (req, res) => {
res.cacheControl = {
noCache: true
};
res.send('This page will not be cached.');
});
// Error handling middleware with specific cache control
app.get('/error', (req, res, next) => {
next(new Error('Something went wrong!'));
});
app.use((err, req, res, next) => {
// Set very short cache for error responses
res.cacheControl = {
maxAge: 5,
noStore: true
};
res.status(500).send(`Error: ${err.message}. This error response caches for 5 seconds.`);
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log('Try visiting / and then /short-cache and /no-cache');
});