Herein are:
- examples of the
fetch
API. - Unit tests for practicing use of
fetch
.
- Clone the repository thus:
git clone https://github.com/portsoc/fetch101.git
- Go into the locally cloned repo:
cd fetch101
- Install libraries (including the http server we use in the next stage):
npm install
- Start a simple HTTP server on port 8080 with:
npm start
- See the examples at http://localhost:8080/examples/
- Unit tests.
- See the unit tests at http://localhost:8080/
- Edit
index.js
; improving and adding code until all the tests pass.
-
fetch_message
- Get
message.txt
, from a server and put in the DOM
- Get
-
fetch_on_button
- When a button is pressed, get
message.txt
, from a server and put in the DOM
- When a button is pressed, get
-
error_handling
- Two buttons are presented. One fetches
message.txt
which is retrieved successfully. The second button retrievesimaginary.file
which does not exist, so a theresponse
object'sok
property (i.e.response.ok
) will be false; an error messsage is therefore displayed.
- Two buttons are presented. One fetches
-
json_data
- When a button is pressed, get
data.json
from a server, parse the data and populate an unordered list with it.
- When a button is pressed, get
-
response_headers
- This extends the error handling example. The same function is used to process three outcomes when different files are loaded. In addition to handlng the non-existent file, the response headers are checked to learn the type of the retrieved data. Different types of data (text and JSON in this example) can be processed differently.
-
other_peoples_data
- Instead of a local JSON file, a remote data file is loaded.
-
interaction
- user input is taken and used to construct a URL.
- the response from the server is used to indicate whether a word is valid in scrabble or not.