0

What are the required http headers to tell the browser to cache an image forever, when sending it via Node.js?

MaiaVictor
  • 51,090
  • 44
  • 144
  • 286

1 Answers1

1

Here we go:

if (!res.getHeader('Cache-Control') || !res.getHeader('Expires')) {
    res.setHeader("Cache-Control", "public, max-age=345600"); // ex. 4 days in seconds.
    res.setHeader("Expires", new Date(Date.now() + 345600000).toUTCString());  // in ms.
}
jmingov
  • 13,553
  • 2
  • 34
  • 37
  • 1
    Just to clarify, this only caches for 4 days, right? That doesn't seem all that permanent to me. – D.Tate Mar 23 '16 at 01:05