From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Unlock this course with a free trial

Join today to access over 25,000 courses taught by industry experts.

Read a file with the fs package

Read a file with the fs package

- So now we have a server that we can send requests to and get people data back from. The next thing we're going to do in order to get a little bit more experience with Node.js and some of the things it lets us do is look at how to read data from a file and send that back to the client. This will also give us a little bit of experience with the async and await keywords. So the first thing we're going to do is inside our source directory, we're going to create a new file and we're going to call this file people-data.json. And what we're going to do is copy and paste this people array into that file. And in order to make it actual JSON, we just have to add double quotation marks around each of these property names. And then we have to delete these trailing commas, since it's not actually legal syntax in JSON files, as well as this trailing semicolon. So now let's save the file. And what we're going to do now is create another endpoint on our server that will load the JSON data from this…

Contents