npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-cache-ctrl

v1.1.8

Published

Express middleware to handle content expiration using Cache-Control header.

Readme

express-cache-ctrl

NPM version NPM Downloads Build Status Issues GitHub forks GitHub stars GitHub license

Express middleware to manage the Cache-Control header, helping browsers with HTTP 1.1 support understand content expiration and when to serve content from the browser's cache. This can significantly improve performance when loading content from your website.

Why use this middleware?

  • Improved Performance: By setting appropriate cache headers, you can reduce server load and decrease page load times for your users.
  • Fine-Grained Control: Easily set different caching policies for different routes or resources.
  • OWASP Recommended: Includes a secure() method to apply secure caching headers as recommended by OWASP.
  • Flexible Configuration: Supports public, private, and no-cache scopes, along with TTLs and revalidation directives.

Installation

npm install express-cache-ctrl

Basic Usage

const express = require("express");
const cache = require("express-cache-ctrl");
const app = express();

// Disable caching for API routes
app.use("/api", cache.disable());

// Set public caching for static assets
app.use("/static", cache.public("1d"));

app.get("/", (req, res) => {
    res.send("Hello World!");
});

app.listen(3000);

API

cache.disable()

Disables caching by setting the Cache-Control header to no-cache, no-store, must-revalidate, proxy-revalidate. Also sets Pragma: no-cache.

cache.secure()

Applies secure caching settings as recommended by OWASP. Sets the Cache-Control header to private, no-cache, no-store, must-revalidate, no-transform.

cache.public(ttl, [options])

Sets the Cache-Control header to public.

  • ttl: Cache Time-To-Live. Can be a number in seconds or a string in ms format (e.g., '1d', '2h'). Defaults to '1h'.
  • options: An optional object for more specific directives.

cache.private(ttl, [options])

Sets the Cache-Control header to private.

  • ttl: Cache Time-To-Live. Can be a number in seconds or a string in ms format (e.g., '1d', '2h'). Defaults to '1h'.
  • options: An optional object for more specific directives.

cache.custom([options])

Returns a middleware with a custom Cache-Control header based on the provided options.

Configuration Options

The public, private, and custom methods accept an options object with the following properties:

  • scope: The caching scope. Can be 'public' or 'private'.
  • ttl: The max-age value in seconds or ms format. Defaults to '1h'.
  • sttl: The s-maxage value in seconds or ms format.
  • mustRevalidate: (Boolean) If true, adds the must-revalidate directive.
  • proxyRevalidate: (Boolean) If true, adds the proxy-revalidate directive.
  • noTransform: (Boolean) If true, adds the no-transform directive.
  • noCache: (Boolean) If true, adds no-cache and no-store directives.

Examples

Setting a default cache policy

You can apply a caching policy to all routes by using the middleware at the top of your Express application.

const express = require("express");
const cache = require("express-cache-ctrl");

const app = express();

// Set a default private cache with a 1-hour TTL
app.use(cache.private("1h"));

app.get("/profile", (req, res) => {
    res.json({ user: "John Doe" });
});

Applying caching to a specific route

You can also apply caching middleware to individual routes.

const express = require("express");
const cache = require("express-cache-ctrl");

const app = express();

// Apply secure caching to a specific route
app.get("/secure-data", cache.secure(), (req, res) => {
    res.json({ data: "This is secure data" });
});

For more examples, please refer to the unit tests in test/cache.js.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

MIT