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

A Response Cache for ExpressJS and RequestUtils

Stats

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

Readme

node-express-request-utils-response-cache

Installation

npm install @mediaxpost/request-utils-response-cache

Usage

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

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.