-
-
Notifications
You must be signed in to change notification settings - Fork 195
Home
Kevin R. Whitley edited this page Oct 29, 2016
·
5 revisions
- How do I see console output for the library?
- 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 })
})
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 })
})