CORS in NestJS: setting up enableCors()
NestJS doesn't send CORS headers any more than Express or Fastify do underneath it. You either pass { cors: true } to NestFactory.create() or call app.enableCors() yourself, and either way it works out to one line. The natural follow-up, given what we found writing about Fastify, is whether that line has to go in a particular place, the way a Fastify hook registered too early can silently break a preflight. We pulled NestJS 12.5.1's core, Express adapter, and Fastify adapter packages from npm and read enableCors() down into the packages each one delegates to.
What enableCors() actually calls
The common pattern looks like this, regardless of adapter:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: ['https://app.example.com'],
credentials: true,
});
await app.listen(3000);
}
bootstrap();
On @nestjs/platform-express, enableCors() is a two-line delegation. The core package's NestApplication.enableCors() calls this.httpAdapter.enableCors(options), and the Express adapter's version of that method is nothing more than:
// @nestjs/platform-express, adapters/express-adapter.js
enableCors(options) {
return this.use(cors(options));
}
cors is a direct, pinned dependency of @nestjs/platform-express 12.5.1 at version 2.8.6, the same package you'd install yourself for a plain Express app. Every default and every gotcha that applies to it applies here unchanged: an unset origin sends *, an unset allowedHeaders reflects whatever the preflight asked for, and pairing credentials: true with that default wildcard produces a response a browser rejects on a credentialed request. We covered that combination, and how to avoid it, in CORS with cookies and credentials, so it isn't repeated here.
Nothing is wired up until listen()
The real question is timing: does app.enableCors(), called after create() but before listen(), actually land ahead of your controllers and guards in the request pipeline? Reading NestFactory.create() answers half of it: it instantiates your modules and providers, but it never binds a single HTTP route. Route binding happens in NestApplication.init(), inside a method called registerRouter(), and init() only runs when something calls it:
// @nestjs/core, nest-application.js
async listen(port, ...args) {
await this.init();
...
}
init() isn't called by create(). It's called by listen(), or by an explicit app.init() if you're not starting an HTTP server at all. So on the Express adapter, calling app.enableCors() anywhere between create() and listen() is safe by construction: the call registers cors() as Express middleware immediately, via this.use(), and your controllers aren't mounted onto that same Express instance until registerRouter() runs later, inside init(), inside listen(). There's no way for a route to end up ahead of it in the stack.
Fastify doesn't get this for free, so NestJS builds it in
Fastify runs its onRequest hooks strictly in registration order, which is exactly what let an auth hook silently swallow a preflight in our piece on setting up @fastify/cors directly: register the auth check before the CORS plugin, and the preflight never reaches it. @nestjs/platform-fastify (also 12.5.1) can't lean on the same "nothing is registered yet" trick that saves the Express adapter, because its own internal request handling, a 503-during-shutdown check, Nest's Express-compatibility middleware layer, security headers, all get wired onto the same Fastify instance too. So the FastifyAdapter's constructor, whether it runs because you passed new FastifyAdapter() to NestFactory.create() yourself or because Nest built one for you, adds a placeholder hook before a single controller, guard, or provider exists:
// @nestjs/platform-fastify, adapters/fastify-adapter.js
this.instance.addHook('onRequest', (request, reply, done) => {
if (!this.corsHook) {
return done();
}
this.corsHook(request, reply, (err) => {
this.copyCorsHeadersToRaw(reply);
done(err);
});
});
A comment directly above that hook in the source states the reason plainly: it must run before Nest's own middleware, so that a preflight is answered before any of it and so the responses it ends still carry the CORS headers. Because the placeholder is added when the adapter object is constructed, before any route, guard, or interceptor exists, nothing registered afterward, in your code or Nest's, can land ahead of it in the hook chain. The actual CORS logic only shows up once your enableCors() call resolves: it loads @fastify/cors, pinned at 11.4.1 against Fastify 5.12.8 in platform-fastify's own package.json, and stores its hook function in this.corsHook for the placeholder to call.
What this guarantees, and what it doesn't
This only fixes ordering relative to Nest's own request handling. A global AuthGuard, an Express-style middleware added with app.use(), or your own Fastify plugin registered after bootstrap can't shadow the preflight response the way a misordered @fastify/cors registration could in a hand-rolled Fastify app. It doesn't change anything about what the underlying cors or @fastify/cors packages send by default, and it won't stop you from setting credentials: true next to a wildcard origin yourself: the Fetch spec rejects that pairing in the browser no matter which framework produced the headers, as covered in CORS with cookies and credentials. What NestJS's adapters remove is the one failure mode that's specific to registration order, not the ones that come from the options you pass.
Through a CORS proxy instead
None of the above applies if there's no NestJS app in the picture, a static frontend calling a third-party API directly is the usual reason to reach for a CORS proxy rather than stand up a backend route just to attach enableCors() to. corsproxy.dev's own Worker always answers with Access-Control-Allow-Origin: * and never sends Access-Control-Allow-Credentials, which keeps it out of the credentialed-request rules above entirely: it's built for calls that carry their own credentials in the request, not ones that need cookies or browser-managed auth to reach the target. Where a dedicated backend route is the better call instead of a proxy is covered in when not to use a CORS proxy.