Skip to content

Instantly share code, notes, and snippets.

@rolfen
Created February 13, 2016 23:39
Show Gist options
  • Save rolfen/e83559873f0ece342412 to your computer and use it in GitHub Desktop.
Save rolfen/e83559873f0ece342412 to your computer and use it in GitHub Desktop.
node: Gets the list of files in a directory and serves it in JSON.
var http = require('http');
var fs = require('fs');
var originalsPath = 'pics/originals';
var originalFiles = {};
fs.readdir('pics/originals',function(err, files){
originalFiles = files;
})
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin' : '*'
});
response.end(JSON.stringify({
"originals" : {
"basePath" : originalsPath,
"fileList" : originalFiles
}
}, null, 2));
}).listen(1337);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment