Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to active caching on Express - Node.js?

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

Thank you

like image 981
dail Avatar asked Jul 04 '11 15:07

dail


People also ask

Does Express have caching?

It'll basically look for a cached value using the request's URL as the key. If it is found, it is sent directly as the response. If it's currently not cached, it'll wrap Express's send function to cache the response before actually sending it to the client and then calling the next middleware.


1 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

like image 88
Alfred Avatar answered Oct 08 '22 01:10

Alfred