Check out a free preview of the full Digging Into Node.js course
The "Fetch Exercise" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:
Kyle instructs students to write a getRecords function that uses fetch to get the JSON data from the API endpoint and then passes the data to a prewritten function to print the formatted results.
Transcript from the "Fetch Exercise" Lesson
[00:00:00]
>> Kyle Simpson: We're good with the server for right now. Let's switch over to our Ex5 web js file. I filled some of this out for you, but you're gonna do a little part of this.
>> Kyle Simpson: The getRecords function, that's gonna be called. It's already linked up with a click handler.
[00:00:18]
That's gonna be called when that button on the page gets called. And what you need to do is make a fetch request to our server endpoint. Remember how we were doing fetch before with,
>> Kyle Simpson: If you haven't used fetch before, it looks like this, where you give it a url.
[00:00:40]
And you can await the response from the url, and then await the JSON on that response. And once you've got the JSON, you should be able to pass it to renderRecords, which will produce some inner HTML for you. It'll produce the inner HTML to print it to the screen.
[00:00:59]
This is basically like our console.table that we did in the previous exercise.
>> Kyle Simpson: So try your hand at writing this fetch code to grab the JSON data from our endpoint.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops