Skip to Content
Senri

CORS

Allow fetch from other origins with cors()

import { ,  } from "@senrijs/senri";

const  = ();

.("/api", ({ : ["https://example.com"], : true }));
  • It answers preflight requests (OPTIONS with Access-Control-Request-Method) with 204 itself
  • It adds CORS headers to other responses. For origins that are not allowed, it adds nothing
  • Unless origin is "*", it adds Vary: Origin
  • origin defaults to "*" (any origin). credentials: true cannot be used with origin: "*" (it throws)
  • With authentication such as basicAuth(), register cors() first, because preflight requests carry no credentials

Decide the origin with a function

.(({ : () => .(".example.com") }));

Returning true allows that origin, and returning a string sends that value. false, null or undefined denies it.

Options

Prop

Type

On this page