skip to Main Content

I’m working on a frontend application using React with Redux and a backend in NestJS with Firebase. I’m facing a CORS issue that I can’t seem to resolve. I’ve configured CORS on the backend, but I’m still getting this error: has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled. in the console. I’m honestly out of ideas on what might be causing this. Any help would be appreciated!

configurated backend

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

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableCors({
    origin: ['http://localhost:3000'],
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // Permitir os métodos GET, POST e PUT
    allowedHeaders: 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Observe, Authorization',
    credentials: true,
  });
  await app.listen(4000);
}
bootstrap();

https://github.com/vsdiaman/backend-chatPDF

I’ve been stuck with this issue for three days now and still no solution. I’ve tried configuring CORS both on the backend and the frontend, but nothing seems to work. I expected the backend configuration to solve the problem, but it hasn’t. I even attempted to adjust the rules in Firebase, but I’m still facing the same issue. I really can’t seem to find a way out of this problem and would appreciate any help. Thanks!

2

Answers


  1. can you add this "{cors:true}"
    I hope it will work

    const app = await NestFactory.create(AppModule, { cors: true });
    
    Login or Signup to reply.
  2. First check your port of your frontend if its the correct link
    Check if it works on localhost:3000 and not on some other port

    From my experience your code looks correct as i have used similar code except not mentioning headers

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search