0

I am unable to enable CORS for NestJs for development, using most of the built-in methods in main.hmr.ts as follows:

const app = await NestFactory.create(AppModule, { cors: true });

const app = await NestFactory.create(AppModule, { cors: { corsOptions... } });

app.enableCors() ;

app.enableCors({ corsOptions.. });

app.use(cors());

app.use( (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Origin', 'Content-Type, Accept');
} );

It feels like everything I'm doing in main.hmr.ts is just being ignored.

The only thing that works is using the @Header decorator i.e. @Header('Access-Control-Allow-Origin', '*') within the controllers.

This is ok as a temporary measure, during development but it's not ideal long term.

I've checked the compiled javascript, and it seems fine. I tried this both on Windows and MacOS; same issue. What did I potentially do wrong?

chaostheory
  • 1,621
  • 4
  • 20
  • 36
  • Does this answer your question? [NestJS enable cors in production](https://stackoverflow.com/questions/50949231/nestjs-enable-cors-in-production) – cyperpunk May 07 '21 at 22:51

1 Answers1

0

I was updating main.hmr.ts which is only used for webpack in production. I should have updated main.ts instead.

chaostheory
  • 1,621
  • 4
  • 20
  • 36