1

I am currently working on a react app but when I run yarn start. I keep getting this issue Access to XMLHttpRequest at 'https://google.com/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

'web-preferences': { 'web-security': false }

but the Moesif Orign & CORS Changer extension on chrome helped me bypass it but I am trying to fix it without an extension.

    const electron = require("electron");
    const app = electron.app;
    const BrowserWindow = electron.BrowserWindow;
    const path = require("path");
    const isDev = require("electron-is-dev");
    let mainWindow;
    let createWindow=()=> {
    mainWindow = new BrowserWindow({ width: 900, height: 680 });
    mainWindow.loadURL(
    isDev
    ? "http://localhost:3000"
    : `file://${path.join(__dirname, "../build/index.html")}`
    );
    mainWindow.on("closed", () => (mainWindow = null));
    }
    app.on("ready", createWindow);
    app.on("window-all-closed", () => {
    if (process.platform !== "darwin") {
    app.quit();
    }
    });
    app.on("activate", () => {
    if (mainWindow === null) {
    createWindow();
    }
    });

I am expecting to bypass this issue with out the Moesif Orign & CORS Changer extension on chrome.

Duke
  • 23
  • 1
  • 3
  • hi @AdamOrlov I added the proxy in package.json ` "devDependencies": { "proxy": "http://localhost:3000"` but I am still having the same issue. 'http://localhost:3000' has been blocked by CORS policy: – Duke Nov 07 '19 at 00:34
  • Does this answer your question? [CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true](https://stackoverflow.com/questions/19743396/cors-cannot-use-wildcard-in-access-control-allow-origin-when-credentials-flag-i) – mrkre Nov 07 '19 at 02:53
  • Not really, I am still having a hard time getting it to work but thanks for trying. – Duke Nov 07 '19 at 13:18

1 Answers1

2

I faced the same problem using expressjs and it's basically the same, here's the code I've used to deal wit CORS

const express = require('express')

const app = express()

// Defining CORS
app.use(function(req, res, next) {
    res.setHeader(
      "Access-Control-Allow-Headers",
      "X-Requested-With,content-type"
    );
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader(
      "Access-Control-Allow-Methods",
      "GET, POST, OPTIONS, PUT, PATCH, DELETE"
    );
    res.setHeader("Access-Control-Allow-Credentials", true);
    next();
});

hope this helps

AIMEUR Amin
  • 179
  • 17