Supplying the Cache-Control header in Express applications

Let's install it with the NPM package manager:

npm install  express-cache-control

Enabling caching with the express-cache-control middleware requires three straightforward steps:

  1. Get the module:
          CacheControl = require("express-cache-control") 
    
  2. Create an instance of the middleware:
          var cache = new CacheControl().middleware; 
    
  3. Bind the middleware instance to the routes you want to enable caching for:
     app.get('/contacts', cache('minutes',1), function(request, response) {   var get_params = url.parse(request.url, true).query;   if (Object.keys(get_params).length == 0)   {     _v1.list(Contact, response);   }   else   {     var key = Object.keys(get_params)[0];     var value = get_params[key];     ...

Get RESTful Web API Design with Node.js - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.