How Node really works · Chapter 18 of 34
An HTTP client
By the end of this chapter you can call other servers from Node safely: check every status, give up on time, retry only what is worth retrying, and stream big downloads to disk. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Networking
- 6 tasks
- Node.js
- In the all labs plan
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.
Why you need this
Tapri's server now talks to other servers. The milk supplier has an API for stock levels, the kitchen server takes new orders, and Ravi's delivery partner reports where each rider is. Some are fast, some are slow, and one goes down every time it rains.
One fetch call is easy. But a supplier that answers 500 looks like success unless you check, a supplier that never answers holds your request for minutes, and a 500 MB nightly report read with text() sits in memory all at once.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
getJson: fetch JSON and treat error statuses as errors
Write getJson(url). It fetches url and returns a promise of the parsed JSON body. For a status outside 200 to 299 it rejects with an Error whose message is 'HTTP <status>' (for example 'HTTP 404') and whose status property is the status code as a number.
- Task 2
postOrder: send a new order to the kitchen server
Write postOrder(baseUrl, order). It sends a POST to baseUrl followed by /orders, with the header content-type: application/json and the body exactly JSON.stringify(order). It returns a promise of the JSON the server sends back. A status outside 200 to 299 rejects just like getJson (the check sends a 400).
- Task 3
getWithTimeout: give up on a supplier that takes too long
Write getWithTimeout(url, ms). It works like getJson, but gives up after ms milliseconds: the promise rejects with the error that AbortSignal.timeout produces, whose name is 'TimeoutError'. The check calls a server that takes 1.5 seconds with ms 100 and expects the rejection well within a second.
- Task 4
getWithRetry: retry a flaky server, but only when it is worth it
Write getWithRetry(url, options), where options is { retries, delayMs }, retries 2 and delayMs 100 when left out. It works like getJson, but after a failure with no response at all (a refused connection) or a 5xx status it tries again, up to retries more times, so at most retries + 1 requests. Between tries it waits delayMs, then 2 * delayMs, then 4 * delayMs and so on.
A 4xx status is never retried: reject at once. When every try fails, reject with the error from the last one (for a 5xx, the 'HTTP <status>' error with its status).
- Task 5 · interview question
Interview: rawGet, a GET request with only node:http
Write rawGet(url) with get from node:http, never fetch (the check makes fetch fail). It returns a promise of { status, headers, body }: the status code, the response's headers object, and the whole body as a UTF-8 string. Any status resolves. The body arrives in pieces, with a ₹ cut between two of them, and must come out whole.
If the request fails, for example a refused connection, reject with that error (its code is 'ECONNREFUSED').
- Task 6
download: save the night's report as it streams in
Write download(url, path, onProgress), where onProgress does nothing when left out. It fetches url and streams the body into a new file at path without holding it all in memory. After each chunk it calls onProgress with the number of bytes received so far, so the numbers only go up and the last is the total. It returns a promise of the total number of bytes.
A status outside 200 to 299 rejects like getJson, before anything is written: no file may be created.
What you will learn
- fetch and statuses: which failures reject, and which resolve.
- Sending JSON: the method, the header and the body.
- Deadlines and retries: AbortSignal.timeout, and backoff.
- http.get: the older, lower-level way, still asked in interviews.
- Streaming a response: the body piece by piece, as it arrives.
More in Networking
Build "An HTTP client" yourself
Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.