Skip to content

Instantly share code, notes, and snippets.

@shime
Created October 9, 2014 20:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shime/5d0287aa454aaebf69da to your computer and use it in GitHub Desktop.
Save shime/5d0287aa454aaebf69da to your computer and use it in GitHub Desktop.
list files in a directory with node.js
var fs = require('fs'),
path = require('path')
var listFiles = function(dir, next){
fs.readdir(dir, function(err, nodes){
if (err) next(err)
next(null, nodes.filter(function(node){
return fs.lstatSync(path.resolve(dir) + "/" + node).isFile()
}))
})
}
// usage: listFiles(dirName)
// example:
// listFiles('./public/images/', function(err, files){
// if (err) throw err
// console.log(files)
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment