node-express-request-utils-response-cache
npm install @mediaxpost/request-utils-response-cache
const ResponseCache = require('@dealerslink/node-express-request-utils-response-cache');
let responseCache = new ResponseCache('responses', {
expire: 300000,
onCacheHit: ((req, res, data) => {
res.set('Content-Type', 'application/json');
})
});
function sendResponse(req, res, next) {
if (!res.headersSent) {
res.set('Content-Type', 'application/json');
for (const header in res.locals.headers) {
if (res.locals.headers.hasOwnProperty(header)) {
res.header(header, res.locals.headers[header]);
}
}
res.status(res.locals.status);
res.json(__.omit(res.locals.body, ['cacheExpiration']));
}
next();
}
app.use(responseCache.handler);
app.use(responseCache.store);
app.use(sendResponse);
See wiki for more details