Skip to content
QriLa | Hyeon Gu edited this page Nov 3, 2022 · 5 revisions

Stand With Ukraine

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 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, 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: []
  }
})
Example 1: Whitelisting successful 200 responses (all codes other than 200 are rejected)
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 cache = 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 })
})