{"id":18190,"library":"cacheable-middleware","title":"cacheable-middleware","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/steveukx/cacheable-middleware","tags":["javascript","cache","headers","express","connect","middleware"],"install":[{"cmd":"npm install cacheable-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add cacheable-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add cacheable-middleware","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default export is the middleware factory function. Named export does not exist.","wrong":"const { cacheable } = require('cacheable-middleware');","symbol":"default function","correct":"import cacheable from 'cacheable-middleware';"},{"note":"CommonJS require returns the default export as a function, not an object with named properties.","wrong":"const { cacheable } = require('cacheable-middleware');","symbol":"default (CommonJS)","correct":"const cacheable = require('cacheable-middleware');"},{"note":"No official TypeScript types. Use `@types/cacheable-middleware` or declare module manually.","wrong":null,"symbol":"TypeScript types","correct":"import cacheable from 'cacheable-middleware';"}],"quickstart":{"code":"const express = require('express');\nconst cacheable = require('cacheable-middleware');\n\nconst app = express();\n\n// Set default cache to 1 hour (3600000 ms) for all /static routes\napp.use('/static', cacheable(3600000));\n\n// Per-route caching using res.cacheFor()\napp.get('/dynamic', (req, res) => {\n  res.cacheFor(60000); // cache for 1 minute\n  res.json({ message: 'this response is cached for 1 minute' });\n});\n\n// Use Moment.js duration syntax (value, unit string)\napp.get('/long', (req, res) => {\n  res.cacheFor(6, 'months');\n  res.send('cached for 6 months');\n});\n\napp.listen(3000);","lang":"javascript","description":"Demonstrates two usage patterns: global cache duration applied to a path and per-response control via res.cacheFor()."},"warnings":[{"fix":"Upgrade Node.js to version 6 or higher.","message":"Version 1.0.0 drops support for Node.js < 6. Requires ES6 features.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Consider using express.static with maxAge option for static files: app.use('/static', express.static('public', { maxAge: '1d' })).","message":"Equivalent functionality exists in HTTP framework built-in features (Express 'static' middleware cache options) and dedicated cache-control modules.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Use valid Moment.js duration formats: number (milliseconds) or number + string (e.g., 6, 'months', 3, 'hours').","message":"Duration units are parsed by Moment.js; invalid strings may throw or produce unexpected durations.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Call res.cacheFor() at the beginning of the route handler, before sending response.","message":"res.cacheFor() must be called before res.send() or res.json() to set headers properly.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use default import: const cacheable = require('cacheable-middleware');","cause":"Importing incorrectly (e.g., named import when using default export).","error":"TypeError: cacheable is not a function"},{"fix":"Run npm install cacheable-middleware and verify package.json.","cause":"Package not installed or typo in package name.","error":"Error: Cannot find module 'cacheable-middleware'"},{"fix":"Ensure app.use(cacheable()) is called before defining routes that use res.cacheFor().","cause":"Middleware not applied before route handler.","error":"TypeError: res.cacheFor is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}