Created
October 30, 2016 23:03
-
-
Save mnyamor/36b8ae338eb0a0428d91f1ce5d8995cb to your computer and use it in GitHub Desktop.
Listing Directory Files
This file contains hidden or 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
| /* | |
| - When we use any methods of the fs module we are given the option to use them synchronously or asynchronously. | |
| - reading files synchronously will block the single Node.js thread so all other connections will wait for this synchronous recal | |
| */ | |
| //include the file system module - we can do just about anything with files and directories | |
| var fs = require('fs'); | |
| // create var for files .. | |
| // var files = fs.readdirSync('./lib'); //READ FILES FROM LIBRARY FOLDER.. | |
| // we're going to synchronously read the lib directory | |
| // console.log(files); | |
| /* You want to take advantage of Node.js's asynchronous nature and not read the files from the directory synchronously. | |
| We can do that simply by dropping the Sync. | |
| Readdir is not going to return our files any longer, but this is an asynchronous command, | |
| so what it's going to do is put in a request to read the files from the library folder | |
| and when the file system is finished reading those files this call back will be invoked. | |
| */ | |
| fs.readdir('./lib', function(err, files) { | |
| if (err) { | |
| throw err; | |
| } | |
| console.log(files); | |
| }); | |
| console.log("Reading Files..."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment