Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static files in Express - is it cached?

In the ExpressJS documentation there is nothing about how the files are loaded.

My question is: when I request a static file, is the file loaded form the disk with every request? Or it is somehow cached? If so, how?

like image 637
David Gatti Avatar asked Mar 13 '23 11:03

David Gatti


2 Answers

Express will not automatically cache static content. For that we need to use https://www.npmjs.com/package/cache-control middleware. More info here (http://blog.modulus.io/nodejs-and-express-static-content) which helps

like image 82
null1941 Avatar answered Mar 23 '23 11:03

null1941


When I request a static file, is the file loaded form the disk with every request?

Most likely, it will not. All modern OS'es cache file data (or rather, disk blocks) in memory, so subsequent accesses to that data are read from memory instead of having to read it off disk again.

This obviously depends on how much free memory your system has, and if the memory is needed for other tasks, the disk data will (gradually) be purged from that cache (least-used data first, usually).

From Node, and therefore Express, you don't have much control over this mechanism, so from that standpoint it looks as if the file is read from disk for every request.

like image 33
robertklep Avatar answered Mar 23 '23 09:03

robertklep