Skip to content
Kevin R. Whitley edited this page Oct 29, 2016 · 5 revisions

FAQ

  1. How do I see console output for the library?
  2. How do I prevent error pages (e.g. 404, 500) from being cached?

### How do I see console output for the library? Just add apicache to the DEBUG environment variable in Node, and witness the beauty that is the [debug](https://www.npmjs.com/package/debug) library. This library has virtually no dependencies, but **debug** was worth including! ``` $ export DEBUG=apicache ``` ### How do I prevent error pages (e.g. 404, 500) from being cached? In version [0.3.0](https://github.com/kwhitley/apicache/commit/850a42d6d01fae67bb5d19fb4f8e05bb752e66b6), statusCode white and black listing support was added to the options/config under the `statusCodes` attribute.
import apicache from 'apicache'
apicache.options({
  statusCodes: {
    include: [],
    exclude: []
  }
})
import express from 'express'
import apicache from 'apicache'
const app = express()

// only statusCode 200 is included
const cache = apicache.options({ statusCodes: { include: [200] }}).middleware

app.get('/someroute', cache('1 week'), (req, res) => {
  res.json({ cached: true })
})

app.get('/failure', cache('1 week'), (req, res) => {
  res.status(404).json({ cached: false })
})
Example 2: Blacklisting only 500 and 404 responses (all other codes are accepted)
import express from 'express'
import apicache from 'apicache'

const app = express()

// only statusCodes 404 and 500 will be rejected
const cacheOnlySuccesses = apicache.options({ statusCodes: { exclude: [404, 500] }}).middleware

app.get('/someroute', cache('1 week'), (req, res) => {
  res.status(400).json({ cached: true })
})

app.get('/failure', cache('1 week'), (req, res) => {
  res.status(500).json({ cached: false })
})
Clone this wiki locally