server.js
// List all files in a directory in Node.js recursively in a synchronous fashion
List all files in a directory in Node.js recursively in a synchronous fashion
List all files in a directory in Node . js recursively in a synchronous fashion
function walkSync(dir, extensions, filelist) {
  if (dir[dir.length-1] != '/') { dir = dir.concat('/'); }
  filelist = filelist || [];
  files = fs.readdirSync(dir);
  files.forEach(function(file) {
    try {
      if (fs.statSync(dir + file).isDirectory()) {
        filelist = walkSync(dir + file + '/', extensions, filelist);
      }
      else {
        var bMatched = false;
        for (var i = 0; (!bMatched) && (i < extensions.length); i++)
        {
          if (file.length >= extensions[i].length) {
            var file_ext = file.substr(file.length - extensions[i].length, extensions[i].length).toLowerCase(); 
            if (file_ext == extensions[i]) {
              filelist.push({ dir: dir, file: file });
              bMatched = true;
            }
          }
        }  
      }
    }
    catch (e) {
      if (e.code === 'ENOENT') {
      } 
      else if (e.code === 'EACCES') {
      } 
      else {
        throw e;
      }      
    }
  });
  return filelist;
}
function walkSync ( dir , extensions , filelist ) { if ( dir [ dir . length - 1 ] != '/' ) { dir = dir . concat ( '/' ) ; } filelist = filelist || [ ] ; files = fs . readdirSync ( dir ) ; files . forEach ( function ( file ) { try { if ( fs . statSync ( dir + file ) . isDirectory ( ) ) { filelist = walkSync ( dir + file + '/' , extensions , filelist ) ; } else { var bMatched = false ; for ( var i = 0 ; ( ! bMatched ) && ( i < extensions . length ) ; i ++ ) { if ( file . length >= extensions [ i ] . length ) { var file_ext = file . substr ( file . length - extensions [ i ] . length , extensions [ i ] . length ) . toLowerCase ( ) ; if ( file_ext == extensions [ i ] ) { filelist . push ( { dir : dir , file : file } ) ; bMatched = true ; } } } } } catch ( e ) { if ( e . code === 'ENOENT' ) { } else if ( e . code === 'EACCES' ) { } else { throw e ; } } } ) ; return filelist ; }
List all files in a directory in Node.js recursively in a synchronous fashion
List all files in a directory in Node . js recursively in a synchronous fashion
//console.log('File not found!'); //console.log('File not acessible!');