Tsallaka zuwa abun ciki
Justice Tech School

fetch in Node

Node 18+ has fetch built in. It returns a promise of a Response.

Example
const http = require("http");
const s = http.createServer((q, r) => r.end(JSON.stringify({ name: "Ada" }))).listen(0, async () => {
  const res = await fetch("http://localhost:" + s.address().port);
  console.log(res.ok, res.status, await res.json());
  s.close();
});

Result

true 200 { name: 'Ada' }

Exercise

Parse JSON.

await res.()

Quiz Test

What does res.ok mean?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.