@dealerslink/node-express-request-utils-response-cache

A Response Cache for ExpressJS and RequestUtils

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@dealerslink/node-express-request-utils-response-cache
1.1.25 years ago6 years agoMinified + gzip package size for @dealerslink/node-express-request-utils-response-cache in KB

Readme

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, // Five minutes
  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();
}

// Later within the expressJS request stack
// Before other processing, check cache
app.use(responseCache.handler);

// Do other processing
// app.use...

// After other processing
app.use(responseCache.store); // This only stores when the req.needsCache is set

// Process the res.locals and send response
app.use(sendResponse);

See wiki for more details