Home » Nodejs » How to active caching on Express – Node.js?

How to active caching on Express – Node.js?

Posted by: admin December 21, 2017 Leave a comment

Questions:

how can i active the caching on Express to provide the pages quickly?

Thank you

Answers:

You should only enable caching in production:

app.configure('production', function(){
  var oneYear = 31557600000;
  app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
  app.use(express.errorHandler());
});

Furthermore you are probably better of using CDN or Nginx to host static files. A lot of CDN’s aren’t that expensive and I even found this free CDN at http://www.coralcdn.org/ thanks to stackoverflow.com. According to this blog post Nginx even has memcached to make your site extremely fast