Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 11.1 — HTTP for JavaScript Developers
JavaScript · Example 1 of 3 ES2017+

A tiny fake server that returns real Response objects

Response is the browser's real response class. ok is true for any status from 200 to 299. A 204 reply has no body, so there is nothing to parse.

HTTP is the request-and-reply conversation between the browser and a server: every request has a method, a URL and headers, every reply has a status code, headers and a body, and the browser's CORS rules decide which replies your JavaScript may read.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a function isRetryable(status) that returns true for 408, 429 and any 5xx status, and false otherwise. Log the result for 404, 429 and 503.

02

Build a Headers object with Accept: application/json and Authorization: Bearer abc, then log every header name, one per line.

Explain it without notes

01

What are the parts of an HTTP request and of an HTTP response?

02

Explain the status code families and give two examples of each.

03

What is the same-origin policy, and how does CORS relax it?

04

When does the browser send a preflight request, and why?

A tiny fake server that returns real Response objects ES2017+

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

GET /api/users/1 -> 200 OK (ok: true)
  body: { id: 1, name: 'Asha' }
POST /api/users -> 201 Created (ok: true)
  body: { id: 2 }
DELETE /api/users/2 -> 204 No Content (ok: true)
GET /api/users/999 -> 404 Not Found (ok: false)
  body: { error: 'Not found' }