Skip to content

Instantly share code, notes, and snippets.

@boxxxie
Last active July 26, 2016 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxxxie/5e5bd524cd395d1ff4c7a5bdf1e123ee to your computer and use it in GitHub Desktop.
Save boxxxie/5e5bd524cd395d1ff4c7a5bdf1e123ee to your computer and use it in GitHub Desktop.
reading in a list of files in nodejs
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);
});
});
});
}
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