0

front-end: react, back-end: node, when the project work on local its work good but when Ι use ngrok and swap the local-url-api by ngrok api-url i see this problem:

Access to XMLHttpRequest at 'API-URL' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. POST "API-URL" net::ERR_FAILED

in node js Ι use cors

app.use(cors({
     origin: "*" ,
     credentials: true,
     optionSuccessStatus: 200
}));
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
  • Make sure to have your `app.use(cors` middleware before registering your routes (before `app.get()` and `app.use('/', ..)` – Fcmam5 Feb 20 '23 at 11:17
  • Does this answer your question? [CORS error but only on POST request, despite cors config (GET have no issue)](https://stackoverflow.com/questions/62374111/cors-error-but-only-on-post-request-despite-cors-config-get-have-no-issue) – O. Jones Feb 20 '23 at 11:24

1 Answers1

0

You can follow this :

Installation

$ npm install cors

then call it from your index.js file :

// Simple Usage (Enable All CORS Requests)
const express = require("express");
const cors = require("cors");
const app = express();

app.use(cors());
Ikram Akbar
  • 120
  • 3
  • 13