Reputation:
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?
Upvotes: 5
Views: 3407
Reputation: 3330
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();
});
Upvotes: 0
Reputation: 203304
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).
Upvotes: 6
Reputation: 106696
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.
Upvotes: 1