Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  27] [ 1]  / answers: 1 / hits: 123496  / 6 Years ago, wed, june 20, 2018, 12:00:00

I've enabled CORS in my NestJS app following the official tutorial, so my main.ts looks like the following:



import { FastifyAdapter, NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule, new FastifyAdapter(), { cors: true });
await app.listen(3000);
}
bootstrap();


and it works when I run the application using npm run start:dev.



However when I try to first compile the application using npm run webpack and then running it using node server.js, the cors will not work.



The http request from the client will fail with:




Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 404.



More From » node.js

 Answers
11

Somehow the issue was compiling it using npm run webpack. If I compile it using prestart:prod then it will work.



Thanks @georgii-rychko for suggesting it via comments.


[#54162] Friday, June 15, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reed

Total Points: 725
Total Questions: 85
Total Answers: 89

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;