-
-
Notifications
You must be signed in to change notification settings - Fork 195
Home
QriLa | Hyeon Gu edited this page Nov 3, 2022
·
5 revisions
- How do I see console output for the library?
- How do I prevent error pages (e.g. 404, 500) from being cached?
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
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: []
}
})
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 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 })
})