CORS
Nest 10 / 11 / 12 · Node 22+ / Bun 1.4+
Packed consumers cover asynchronous origin callbacks, credentials, reflected
preflight headers, preflightContinue, callback errors and concurrent
allowed/denied origins on Nest 10/11/12 and Node/Bun. Returning false from
the origin callback suppresses CORS headers but does not block the controller:
CORS is a browser policy, not server-side authorization.
Browser cross-origin access is off until you call app.enableCors(), as on
Express. app.enableCors(options) and NestFactory.create(..., { cors }) take
Nest’s CorsOptions or a CorsOptionsDelegate and behave like the cors
package that Nest’s Express adapter installs.
- The adapters merge options over the
corsdefaults (any origin,GET,HEAD,PUT,PATCH,POST,DELETE, status204). Anoriginthat is present butundefinedoverrides the default, so{ origin: process.env.CORS_ORIGIN }with the variable unset turns CORS off instead of allowing every origin.enableCors()reads static options once, so later changes to the object have no effect. originmay be*,true(reflect the request origin), a string, aRegExp, an array of those, or a callback. The callback also runs for requests without anOriginheader and receivesundefined. An error it passes becomes a 500 response, as on Express.methods,allowedHeaders(aliasheaders) andexposedHeaderstake strings or arrays. An emptymethodsorexposedHeaders(""or[]) andallowedHeaders: []send no header. WithoutallowedHeaders, or withallowedHeaders: ""(unset, as incors), the adapters reflect the requested headers and addAccess-Control-Request-HeaderstoVary.- The adapters honor
credentials,maxAge,preflightContinueandoptionsSuccessStatus. They answer everyOPTIONSrequest as a preflight unlesspreflightContinueis set. In that case the request reaches the@Options()route with the CORS headers already set. - A delegate may call back with no options to get the defaults. Only delegates and origin callbacks run per request.