How do I cache files served with express.static in memory?
Asked Answered
D

3

5

According to this question express.static reads files from the harddrive every time. I'd like to cache served files in memory, since they won't be changing, there aren't many and I have plenty of memory to do so.

So for code such as this:

// serve all static files from the /public folder
app.use(express.static(path.join(__dirname, 'public')))

// serve index.html for all routes
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'public/index.html'))
})

How do I make sure that express caches files served through express.static and res.sendFile in memory?

Diligence answered 8/3, 2017 at 10:26 Comment(5)
you can see this : cache staticLabbe
@BougarfaouiElhoucine That's the question I already linked. There's no answer to my question there.Diligence
There's also this question: #35068971. But the top answer concerns clientside caching, not serverside (which is what I'm after).Diligence
The OS will take care of that, when it has plenty of RAM it will use that to buffer file system data.Houseboy
@Houseboy If you could elaborate on that it would make a good answer.Diligence
H
6

This usually isn't worth the trouble, as the operating system will take care of this for you.

All modern operating systems will use unused RAM as "buffer cache" or "page cache". Recently used file system data will be stored there, in RAM, so once a file has been loaded into memory any subsequent reads will be served from memory instead of actually being read from disk.

The advantage of relying on this is that the OS will automatically purge data from the buffer cache when there happens to be an increase in memory consumption from processes, thus not running the risk of memory-starving those processes (as you might have when you implement something in user space yourself).

Houseboy answered 8/3, 2017 at 11:53 Comment(0)
W
1

The short answer is you can't, at least not with express.static(). You will need to use a third party module or write your own. Additionally, you could open a feature request issue on the appropriate issue tracker asking for some sort of hook to intercept calls to read a requested file from disk.

Warnock answered 8/3, 2017 at 10:53 Comment(0)
E
0

One way to do this is to read the HTML file when Node starts up and serve the HTML string from a variable. Here is an example using Express. Replace MY_DIST_FOLDER with your folder location.

//using Express
const fs = require('fs');
const express = require('express');
const app = express();

//get page HTML string
function getAppHtml() {
  let html = '';
  try {
    html = fs.readFileSync(`${MY_DIST_FOLDER}/index.html`, 'utf8');
  } catch (err) {
    console.error(err);
  }

  return html;
}

let html = getAppHtml();

//serve dist folder catching all other urls
app.get(/.*/, (req, res) => {
  if (html) {
    res.writeHead(200, {'Content-Type': 'text/html','Content-Length':html.length});
  } else {
    html = "Node server couldn't find the index.html file. Check to see if you have built the dist folder.";
    res.writeHead(500, {'Content-Type': 'text/html','Content-Length':html.length});
  }
  res.write(html);
  res.end();
});
Edmondo answered 31/3, 2021 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.