Skip to content
Justice Tech School

Headers and CORS

Set headers with setHeader. CORS headers let browsers call your API from other origins.

Example
const http = require("http");
const s = http.createServer((req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("X-App", "demo");
  res.end("ok");
}).listen(0, async () => {
  const r = await fetch("http://localhost:" + s.address().port);
  console.log(r.headers.get("x-app"), r.headers.get("access-control-allow-origin"));
  s.close();
});

Result

demo *

Exercise

CORS header.

Access-Control-Allow-

Quiz Test

What does CORS control?

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