Node.js comes with a standard module for interacting with the file system. In this training video, Alex Banks will show you how to list files from a directory, as well as how to synchronously read configuration files when the app is launched, and output the contents by logging the files.
- Node.js also ships with a module that allows us to interact with the file system. The fs module can be used to list files and directories, create files and directories, stream files, write files, read files, modify file permissions or just about anything that you need to be able to do with the file system. Let's get started by listing the contents of a current directory. Let's go ahead and navigate to our files. In start you will notice that we have lib directory. So we are going to list the contents of this directory. It contains a file called people.json, sayings.md and a sub-directory scripts, and in the sub-directory are the alwaysTalking.js file and the spawn.js file from the last lesson.
So we're going to synchronously read the lib directory, so the contents of the lib directory. The next thing that we're going to go ahead and do is just output those contents by logging the files. So, let's go ahead and save and run this. We can go back over to our terminal. Now I can simply node list, and we can see that we get a list of everything that we find inside of the lib directory as an array. We'll notice that it also found that hidden .DS-Store file inside of that directory.
You also may notice that our files have an extension but our sub-directories do not. Let's navigate back to our code. So notice that when we read these files we are using the readdirSync command. This means that I read the contents of the directory synchronously with a blocking request. By the time we get to line five we should actually have the files. When we use any methods of the fs module we are given the option to use them synchronously or asynchronously. Be careful because reading files synchronously will block the single Node.js thread so all other connections will wait for this synchronous recall.
We usually like to read configuration files synchronously when we start the app. 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. When we drop the Sync from any of these fs commands, also 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.
Now, inside of this call back, if there was an error reading the files from the directory that will be the first argument that's passed to this call back. Otherwise, the files themselves will be passed as the second argument. In this case, if there is an error I'm just going to go ahead and throw it, and otherwise instead of logging the files down here I'm going to go ahead and log the files within the call back. So this happens asynchronously which means we can log down here "Reading Files..." So the single threaded nature of Node.js will come in here and will place a command on line three to read the contents of the library directory.
Then it will go on the doing other things. It will go down to line 13 and console.log("Reading Files...") Once the file system has actually read the files and we have results, the call back function that we added as the second argument to the readdir function will be invoked. This call back will pass an error if any errors occur during the process of reading files and the actual files from the directory that were read. Let's go ahead and save this and come back out to our terminal and we can run this again.
And this time we can see Reading Files... occurs first and then we see the same array of files. So we're doing the exact same thing except we are doing this asynchronously. If we go to the api documentation for the file system what you will notice is that every single one of these commands has a synchronous version and an asynchronous version. If we look at this we can see that we can get file statistics synchronously and asynchronously. We can go ahead and un-link files synchronously and asynchronously.
We can remove directories synchronously and asynchronously. We have the option to use the synchronous command or the asynchronous command. Again, we like to use the synchronous command when we start our applications. So if we're reading configuration details, so on and so forth, but when we are actually running our applications, we want to stay away from those synchronous commands and make sure we are putting in asynchronous commands.
Alex Banks shows how to install Node.js on a Mac or PC and work with the Node.js core: the global object, event loop, http module, and file system. Then he covers reading and writing data, streaming data, making http requests, and working with the node package manager (npm). In the final chapters, he shows how to start sessions on web servers, communicate with web servers and clients with WebSockets, use the Express framework to develop applications, and test and debug Node.js code. Want to speed up your Node.js workflow? Check out the tutorials on automating tasks with Grunt and npm scripts.
- What is Node.js?
- Installing Node.js
- Understanding the global object and global timing
- Importing the core modules
- Handling events
- Creating child processes
- Reading, writing, and removing files
- Working with file streams
- Making http requests
- Serving files and JSON data
- Installing npm, the node package manager
- Working with Express, the web server framework
- Using WebSockets
- Testing and debugging Node.js code
- Working with Grunt and Browserify
- Automating tasks with npm scripts