# express-cache-controller

> Middleware for meddling with Cache-Control headers

Latest version **1.1.0** (published 2017-10-30) · MIT license · 5.3K weekly downloads

## Install

```sh
npm install express-cache-controller
pnpm add express-cache-controller
yarn add express-cache-controller
bun add express-cache-controller
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2017-10-30 |
| First published | 2016-05-21 |
| Weekly downloads | 5.3K |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 18 |
| Author | Christopher Sidebottom |
| Maintainers | damouse404 |
| Keywords | express, max-age, maxage, cache, cache-control, no-cache |

## Links

- npm: https://www.npmjs.com/package/express-cache-controller
- Repository: https://github.com/DaMouse404/express-cache-controller
- Homepage: https://github.com/DaMouse404/express-cache-controller#readme
- Issues: https://github.com/DaMouse404/express-cache-controller/issues
- npm.io page: https://npm.io/package/express-cache-controller

## Dependencies (2)

- [on-headers](https://npm.io/package/on-headers.md) ^1.0.1
- [lodash.isnumber](https://npm.io/package/lodash.isnumber.md) ^3.0.3

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads
- [@magnetarjs/plugin-vue3](https://npm.io/package/@magnetarjs/plugin-vue3.md) — 790 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2017-10-30
- 1.0.1 — 2016-05-23
- 1.0.0 — 2016-05-21

## README

# express-cache-controller

A simple and lightweight module for managing cache control headers from within your application. It also tries to provide a simple set of rules for common use cases such as setting 'max-age=0' when 'no-cache' is present by default.

## Example
Configuring noCache easily:
```js
app.use(cacheControl({
  noCache: true
}));
```
Creates a cache-control header of `no-cache, max-age=0`

## Usage
To start using cacheControl, just use the middleware in your application:
```js
app.use(cacheControl());
```

### Default Cache Headers
When initialising the middleware you can set default options when you use it in your application:
```js
app.use(cacheControl({
    maxAge: 5
}));
```

### Overriding Defaults
Just set the cacheControl property of the response object after the cacheControl() middleware is loaded:
```js
app.use(cacheControl({ maxAge: 60 }));
app.get('/', function (req, res, next){
  res.cacheControl = {
      maxAge: 30
  };

  res.send('hai');
});
```

This is useful in error conditions where you can setup cache headers before and after a request is processed:
```js
app.use(cacheControl({ maxAge: 60} ));
app.get('/', function (req, res, next) {
  next(Error('BOOM!'));
});
app.use(function (err, req, res, next) {
  res.cacheControl = {
      maxAge: 5
  };

  res.status(500).send('oh no!');
});
```

## Options
Name | Value | Description
----|----|---
private | Boolean | Adds 'private' flag, overrides 'public' option
public | Boolean | Adds 'public' flag
noStore | Boolean | Adds 'no-store' flag and includes noCache
noCache | Boolean | Adds 'no-cache' flag, sets maxAge to 0 and removes sMaxAge, staleIfError and staleWhileRevalidate
noTransform | Boolean | Adds 'no-transform' flag
mustRevalidate | Boolean | Adds 'must-revalidate' flag and removes staleIfError and staleWhileRevalidate
staleIfError | Number | Adds 'stale-if-error=%d' flag
staleWhileRevalidate | Number | Adds 'stale-while-revalidate=%d' flag
maxAge | Number | Adds 'max-age=%d' flag
sMaxAge | Number | Adds 's-maxage=%d' flag

---
_Source: https://npm.io/package/express-cache-controller · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
