Implement Caching Server with Express.js custom middleware

In this article, we explored the capability of expressjs custom middleware. We have made a minimal cache server implementation in expressjs middleware, which will serve as cache for link GET web requests. So below is the main server code implementation with inline documentation.

How To Buy A Term Paper Online Server.js

 

  var express = require('express');
  var app = express();
  var cache = require('./cacheServer');  custom watermark paper canada // the Cache Server middleware
  var cached = [];  http://www.apisalud.es/?how-to-write-an-admission-essay // Array where the cache to be saved in memory
  var pathname = '';
  var url = require('url');
  var cacheHelper = require('./cacheHelper');  go // cache method utility

  app.use(cache(cached)); / diwali festival essay for kids / injection of the middleware

  app.get('/', function (req, res) {
    cacheHelper.setCache(req, res,cached,"Hello World");  go to link // serving request and save in cache
    res.send('Hello World!');
  });

  app.get('/about', function (req, res) {
    cacheHelper.setCache(req, res,cached,"Hello about");  i can write my essay // serving request and save in cache
   res.send('Hello about!'); 
  }); 

  var server = app.listen(3000, function () { 
    var host = server.address().address; 
    var port = server.address().port; 
    console.log('Application is running at http://%s:%s', host, port); 
})

Below is main implementation of cache server. Here the the request is first checked within the cache array object. If this cache content is found, it will be served from cache, otherwise the request will be forwarded to main request serving process for the server.

click here cacheServer.js


  var cacheHelper = require('./cacheHelper.js');

  // the middleware function
  module.exports = function(cached) {

      return function(req, res, next) {
          var resource = cacheHelper.getCache(req, res,cached);  here // get the content from cache
          if(req.method == 'GET')  go site // cache is implemented only for 'GET' method
	  {
	  	if (typeof resource !== 'undefined') {
 	  	    res.end(resource);  go // serve from cache
	  	}
		else {
	 		next();  submit essays for money // forward to fresh request processing
		}

          }
          else {
	  	next();  research in essay // forward to fresh request processing
	  }
      }	

  };

Below is a helper class for get object from cache and set object to cache.

law dissertation help cacheHelper.js

 
  var url = require('url');
  var pathname = '';

  function getCache(req,res,cached)
  {
	pathname = url.parse(req.url).pathname;
        return cached[pathname]; 
  }

  function setCache(req,res,cached,content)
  {
	pathname = url.parse(req.url).pathname;
        cached[pathname] = 'cached response' + ' from '+ pathname + ' ' + content;   
  }

  exports.getCache = getCache;
  exports.setCache = setCache;

We expect reader to be habituated with node.js and express.js coding.

If you find this article helpful, you can connect us in  Google+ and Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *