Tsallaka zuwa abun ciki
Justice Tech School

Reading a Request Body

Collect chunks from the request stream, then parse them.

Example
const http = require("http");
const s = http.createServer((req, res) => {
  let body = "";
  req.on("data", (c) => (body += c));
  req.on("end", () => { const d = JSON.parse(body); res.end("got " + d.name); });
}).listen(0, async () => {
  const r = await fetch("http://localhost:" + s.address().port, { method: "POST", body: JSON.stringify({ name: "Ada" }) });
  console.log(await r.text());
  s.close();
});

Result

got Ada

Exercise

Event with chunks.

req.on("", fn)

Quiz Test

Why collect chunks?

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