Last active
July 26, 2016 01:35
-
-
Save boxxxie/5e5bd524cd395d1ff4c7a5bdf1e123ee to your computer and use it in GitHub Desktop.
reading in a list of files in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
function readFiles(dirname, onFileContent, onError) { | |
fs.readdir(dirname, function(err, filenames) { | |
if (err) { | |
onError(err); | |
return; | |
} | |
filenames.forEach(function(filename) { | |
fs.readFile(dirname + filename, 'utf-8', function(err, content) { | |
if (err) { | |
onError(err); | |
return; | |
} | |
onFileContent(filename, content); | |
}); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
function readFiles(dirname, onFileContent, onError) { | |
files = fs.readdirSync(dirname).map(function(filename){ | |
return [filename, fs.readFileSync(dirname + filename, 'utf-8')]; | |
}) | |
.map([filename, JSON.parse]); | |
filesObj = //compile array into {'filename': obj ... } | |
return fileObj; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment