{"id":18328,"library":"express-cache-headers","title":"express-cache-headers","description":"An Express middleware for setting response cache headers. Version 0.1.4 is the latest stable release. It allows you to control cache behavior per-route by providing options such as TTL (time-to-live), nocache, private, and mustrevalidate. Unlike more complex caching solutions like apicache or express-cache-ctrl, this package focuses solely on setting cache HTTP headers (Cache-Control, Pragma, Expires) with minimal configuration. It is lightweight and does not require a store, making it ideal for simple caching scenarios where only headers are needed.","status":"active","version":"0.1.4","language":"javascript","source_language":"en","source_url":"git://github.com/nitsujlangston/express-cache-headers","tags":["javascript","cache","header","middleware"],"install":[{"cmd":"npm install express-cache-headers","lang":"bash","label":"npm"},{"cmd":"yarn add express-cache-headers","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-cache-headers","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency; middleware expects Express application","package":"express","optional":false}],"imports":[{"note":"The package is CJS only; it does not support ESM (no default export). Using import will result in an error.","wrong":"import cache from 'express-cache-headers';","symbol":"cache","correct":"const cache = require('express-cache-headers');"},{"note":"The middleware expects a single argument (number or object). A second argument is ignored.","wrong":"app.use(cache(10, 'optional'));","symbol":"cache","correct":"app.use(cache(10));"},{"note":"ttl must be a number in seconds; using a string may cause unintended behavior.","wrong":"app.get('/route', cache({ttl: '300'}), handler);","symbol":"cache","correct":"app.get('/route', cache({ttl: 300, private: true}), handler);"}],"quickstart":{"code":"const express = require('express');\nconst cache = require('express-cache-headers');\n\nconst app = express();\nconst port = process.env.PORT || 3000;\n\n// Global middleware: cache all routes for 10 seconds\napp.use(cache(10));\n\n// Override per route: no cache\napp.get('/nocache', cache({nocache: true}), (req, res) => {\n  res.send('This response is not cached');\n});\n\n// Route with custom TTL and private directive\napp.get('/private', cache({ttl: 300, private: true}), (req, res) => {\n  res.send('Private, cached for 5 minutes');\n});\n\napp.listen(port, () => console.log(`Server listening on port ${port}`));","lang":"javascript","description":"Demonstrates global and per-route use of cache middleware with ttl, nocache, and private options."},"warnings":[{"fix":"Do not use both global and per-route cache middleware simultaneously; choose one pattern.","message":"When using cache as global middleware, subsequent per-route cache calls will merge/override the global settings, but the global middleware still runs and may set headers before the route-specific middleware.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"If actual response caching is needed, use a package like apicache or express-cache-ctrl instead.","message":"The middleware sets Cache-Control, Pragma, and Expires headers. It does not integrate with any store; cached responses are not actually stored or served from cache—only headers are set.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Review if you need HTTP/1.0 compatibility; if not, consider manually setting Cache-Control headers.","message":"The `nocache` option sets Cache-Control: no-cache but also sets Pragma: no-cache and Expires to a past date. This may not be appropriate for all scenarios (e.g., HTTP/1.0 compatibility).","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Replace `import cache from 'express-cache-headers'` with `const cache = require('express-cache-headers');`","cause":"Using ES import syntax with this CJS-only package.","error":"TypeError: cache is not a function"},{"fix":"Pass a number or options object: `app.use(cache(10));` or `app.use(cache({ttl: 10}));`","cause":"Calling `app.use(cache())` without a default option or incorrect call.","error":"Error: cache is not a function"},{"fix":"Set custom Cache-Control headers before the cache middleware or avoid overriding them.","cause":"User sets custom Cache-Control after middleware.","error":"Warning: Cache-Control headers might be overwritten by res.set()"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}