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 - JavaScript Tutorial
From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext
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
-
-
-
-
-
-
-
-
-
-
-
(Locked)
Learning objectives1m 34s
-
(Locked)
Learn the basics of writing a Node server3m 52s
-
(Locked)
Create and set up a Node.js project3m 59s
-
(Locked)
Create and run a basic Express server5m 47s
-
(Locked)
Create and test a GET endpoint6m 27s
-
(Locked)
Read a file with the fs package4m 40s
-
(Locked)
Create and test a POST endpoint8m
-
(Locked)
-
-
-