{"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,"search_vec":"'0.1.4':14 'allow':21 'apicach':50 'behavior':26 'cach':3,11,25,47,54,62,66,87,95 'cache-control':65 'complex':46 'configur':72 'control':24,67 'ctrl':55 'expir':69 'express':2,6,53 'express-cache-ctrl':52 'express-cache-head':1 'focus':58 'header':4,12,64,91,96 'http':63 'ideal':84 'javascript':94 'latest':17 'lightweight':75 'like':49 'live':39 'make':82 'middlewar':7,97 'minim':71 'mustrevalid':43 'need':93 'nocach':40 'option':32 'packag':57 'per':28 'per-rout':27 'pragma':68 'privat':41 'provid':31 'releas':19 'requir':79 'respons':10 'rout':29 'scenario':88 'set':9,61 'simpl':86 'sole':59 'solut':48 'stabl':18 'store':81 'time':37 'time-to-l':36 'ttl':35 'unlik':44 'version':13","created_at":"2026-04-25T07:37:39.894536+00:00","updated_at":"2026-04-25T07:37:39.894536+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/nitsujlangston/express-cache-headers","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/express-cache-headers","openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}