Join Alexander Zanfir for an in-depth discussion in this video Read from files, part of Learning Node.js.
(Male Narrator) - There are two areas where heavy input/output takes place. Network and disc access. So let's take a look at disk access to begin with by working with some files. We'll start with reading files and then move onto writing our own files. In order to do that we're going to require access to the file system that's built with the node. But first let's start by creating a new file. I'll call this demo.js. Now let's require in the file system and I'll call it fs for short.
And as you can see, the library itself is also called fs. The next command we'll perform will be to read from the file but we don't have a file to read yet so let's create a temporary JSON file and put some data into it. I'll call it data.json and in here we'll simply create an object that has a property name. And we need to wrap the property in double quotes. Then I'll use a colon and I'll use the name Tim as a place holder.
Let's close that out. The first thing we'll do is access the file system with a function called, read file. (typing) And now let's pass in the location of our data JSON file. The second parameter since this is an asynchronous function will be our call back. So I could define a separate function like we did in our asynchronous video or I could provide an anonymous function and place it in the second parameter directly.
The callback will pass an error and then the data that's read in. And so that's another way we can handle the call back. Another way yet would be to use an arrow function. And it's slightly more compact than the previous syntax. So we'll use that going forward. For now, let's go ahead and console log the data we get and see what happens. (typing) And then I'll use nodemon to execute our demo.
As you can see we got some sort of output with a buffer at the start. That's not what we have inside our JSON. So what's going on? What's happening is we didn't specify the file format. And in order to read the JSON file and other documents we need to specify the UTF 8 format. So instead of the callback being the second parameter we'll push it over to the third parameter. And then we'll specify, as a string, UTF-8 as a second parameter.
Let's save that and as you can see we're able to read in the JSON just fine. Another good to know is that we can actually access our JSON file with a require directly instead of using read file. Let's try that out now. So I'll go to line two and create a data variable set it to require and specify the path to data.json. Then let's console log data. (typing) I'll save that.
And now you can see we're getting two objects in our console. One from the require and one from our read file. They look a bit different so let's see what those differences are. The first thing I'm going to do is try to access the property name from our require to see if it's a true object or just a string. So I'll go over to line four and type in dot name. If I save that we can see it's outputting just the name. And so we're able to access the property and therefore it is an object.
Let's try to do the same for our read file. As you can see, it's undefined. Therefore it is just a string. So let's create a new variable inside our read file callback called data and set it to json.parse. And then I'll pass in data. So, essentially it's taking our data parameter converting it to JSON and then overriding it. And then we're able to access data.name.
And if we look in our console we can see there are two names being displayed.
- Asynchronous tasks and callbacks
- Managing third-party packages with npm
- Node.js frameworks
- Static serving with Express
- Creating a browser app
- Exploring databases
- Saving data to MongoDB with Mongoose
- Error handling and debugging
- Simple tests and async tests