Start learning free

LESSON 1 OF 173 · BACKEND: HOW THE WEB WORKS

Your First Request

Practice

Every time a browser opens a page, it sends a request to a server, a program waiting on some computer, and the server sends back a response. A JavaScript program can do the same with the built-in function fetch.

fetch(address) sends the request and gives back the response. A response takes a moment to arrive, so you write await before fetch, inside a function marked async. A response starts with a status code, a number that says how it went (200 means OK), and then comes the body, the content itself. .status gives the number, and .text(), also with await, gives the body as a string.

const page = await fetch("https://example.com/about");
console.log(page.status);
const body = await page.text();
console.log(body);

The shop's supplier runs a server at the address in SUPPLIER. Its home page is the path /. When your program asks for it, it must print:

Status: 200
Text: Supplier API. Try /prices

Your task: inside main, send a GET request to the supplier's home page, keep the answer in response, then print its status code and its text, each after its label.


Assignment
Follow the Coding Tutorial to send your first request and print the status code and the text that come back.


Hint
The address is a string: join SUPPLIER and the path with +. console.log can take a label and a value, separated by a comma. Both fetch and response.text() need await.


STEP 1 OF 3
    Ctrl + Enter to check