I would like to know if the following code would cache the file's contents in the server's memory after reading it once. The reason I ask is because I don't want to have to re read the file every time the user requested the page. I would prefer to have it cached after the first read.
fs.exists(fileName, function (exists) {
if (!exists) {
console.log("== 404 error");
resp.writeHead(404, {'Content-Type': 'text/html'});
resp.end(pageError);
return;
}
fs.readFile(fileName, 'utf8', function (err, data) {
if (err) {
resp.writeHead(404, {"Content-Type": "text/html"});
resp.end(pageError);
return;
}
var contentType = getContentType(req.url);
var mimeType = mimeTypes[contentType];
resp.writeHead(200, {"Content-Type": mimeType});
resp.end(data);
});
});
NOTE ** I only want to know how to do this using internal Node JS modules (no express)