Express Cache Controller Middleware

raw JSON →
1.1.0 verified Thu Apr 23 auth: no javascript abandoned

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.

error TypeError: require(...) is not a function
cause Attempting to call the result of `require('express-cache-controller')` as a function directly, when it's already the middleware function itself.
fix
Correct usage is const cacheControl = require('express-cache-controller'); app.use(cacheControl(options));
error Cache-Control header is missing or incorrect in response.
cause The `express-cache-controller` middleware was not applied, or `res.cacheControl` was set incorrectly/too late, or another middleware overwrote the header.
fix
Ensure 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
cause Attempting to use `cacheControl` in an ESM module context without a CommonJS `require` statement.
fix
This package is CommonJS. Use 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.
breaking The package is CommonJS-only. Attempting to use `import` syntax in an ESM module without proper transpilation or configuration will result in errors.
fix Ensure your project uses CommonJS (`require`) for importing `express-cache-controller` or configure your build system to handle CommonJS modules in an ESM context.
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.
fix For new projects or applications requiring ongoing support and security, consider more actively maintained Express caching solutions or implementing `Cache-Control` headers manually.
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.
fix If server-side caching is required to reduce backend load or improve response times by serving pre-computed results, integrate a separate server-side caching solution (e.g., Redis, `node-cache`, `express-response-cache`).
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.
fix Place `express-cache-controller` middleware early in your Express application's middleware stack. If specific routes need different headers, set `res.cacheControl` *after* this middleware has run, but before `res.send()` or `res.end()`.
npm install express-cache-controller
yarn add express-cache-controller
pnpm add express-cache-controller

This quickstart demonstrates setting global `Cache-Control` defaults, overriding them per-route, disabling caching, and applying specific cache settings to error responses.

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');
});