We've explored the fundamentals of JavaScript and had a refresher of HTML and CSS. Now that we're familiar with the DOM, let's take this a step further and attempt to bring all our learning together.
Using an external API, let's begin constructing a front-end app which populates a page with dynamic information. You'll be using fetch() requests, we're talking DOM manipulation and we're talking user input.
This afternoon lab may seem quite intimidating so we're going to first lay out a start point for you. Instead of providing one, we're going to talk through the steps to get you familiar with the code patterns:
-
Step 1: HTML
- Open up VSCode and create a new HTML file
- Using Emmet abbreviation (
!orhtml:5) or by typing it yourself, set up a basic HTML template - Add a heading (
h1) to thebodyfollowed by asection - Give your
sectiona distinctid
-
Step 2: JS
- Again within VSCode, create a new JS file
- Move back to your HTML file and connect your JS file using the
scripttag and your filepath in thesrcattribute - Remember to include
deferto yourscripttag so it runs after the HTML loads! - Back in your JS file, add in a simple
console.log("HI!")
-
Step 3: In the browser
- Open up your HTML file in the browser by right-clicking your file and then selecting
Open With > Google Chrome - Your webpage will be blank save the heading added earlier
- Right click the page in the browser and select
Inspect - At the top right, select the
Console - Check that your
"HI!"statement is printed there - If the message doesn't appear, check that your files are saved and that the filepath in the
scripttag is correct
- Open up your HTML file in the browser by right-clicking your file and then selecting
-
Step 4: Familiarisation of the Countries API
- Have a look at API endpoint which returns all held information on all countries within their set: https://restcountries.com/v3.1/all
- This will be difficult to parse so let's install the Chrome JSON Viewer extension https://chromewebstore.google.com/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh?pli=1
- Refresh the endpoint page to see the output in a clearer format
- Familiarise yourself with the rough structure of the output
- Now, let's visit another endpoint https://restcountries.com/v3.1/name/peru
- Familiarise yourself with this structure and consider how this relates to the previous endpoint's output
fetch()a single country
- In your JS file, create a function called
getCountryByName() - Give this function one argument,
countryName - Within the function place a
fetch()request which points towards thehttps://restcountries.com/v3.1/name/{countryName}endpoint (you will need to choose which country to display). Follow the pattern we demonstrated in thePromises Intro & Fetchlesson (which uses the Random Dog API). - Call your function at the bottom of your JS file, passing in a valid country name as an argument
- Log the result in the console to check all is working as you would expect
- Take the output from your
getCountryByName()function and make use of it on the webpage by displaying your country'sname,capital,languagesandpopulation(NB: many countries have more then one language and your code should account for anycountryNamebeing passed in.
- Neaten up your JavaScript functionality, making abstract functions for any repeated functionality
- Create a
getAllCountries()function which displays the details of every country to the frontend - Make your page display an error message should it meet an error on querying the API. Test this out by trying to access an endpoint which doesn't exist for the API
TIPS
- Remember you can use dot notation (
object.property) to access the value of a specifiedproperty- The MDN Documentation has all you may need on JavaScript functionality. If you're unsure on any part, start there
- The RESTCountries API has multiple values under the
nameproperty. You will most likely want to usename.common- You can check whether a fetch request is successful or not by accessing the
okproperty of the response which is eithertrueorfalse- When creating functionality tied to the appearance of an error, this should be housed within a
try catchblock