server.js
//Accessing memory storage (RAM ) is faster than accessing the filesystem.  Node application cache frequently used data in memory.
Accessing memory storage (RAM ) is faster than accessing the filesystem.  Node application cache frequently used data in memory.
Accessing memory storage ( RAM ) is faster than accessing the filesystem . Node application cache frequently used data in memory .
function serveStatic(response, cache, absPath) {
  if(cache[absPath]){
    sendFile(response, absPath, cache[absPath]);
  }else {
    fs.exists(absPath, function(exists){
      if(exists){
        fs.readFile(absPath, function(err, data){
          if(err) {
            send404(response);
          }else {
            cache[absPath] = data;
            sendFile(response, absPath, data);
          }
        });
      }else {
        send404(response); 
      }
    });
  }
}
function serveStatic ( response , cache , absPath ) { if ( cache [ absPath ] ) { sendFile ( response , absPath , cache [ absPath ] ) ; } else { fs . exists ( absPath , function ( exists ) { if ( exists ) { fs . readFile ( absPath , function ( err , data ) { if ( err ) { send404 ( response ) ; } else { cache [ absPath ] = data ; sendFile ( response , absPath , data ) ; } } ) ; } else { send404 ( response ) ; } } ) ; } }
Accessing memory storage (RAM ) is faster than accessing the filesystem.
Accessing memory storage ( RAM ) is faster than accessing the filesystem .
//check if file is cached in memory //Serve file from memory //check if file exists //read file from disk //serve file read from disk //send http 404 response