Wednesday, 2 September 2020

How to fix "from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." in my express server

Hi,I have an express server that I have some routes.Here is how my express server is setup on serve.rjsapp.use(session( { name: 'session-id', secret: ['secret'], resave: false, saveUninitialized: false, cookie: { secure: false, sameSite: 'none', httpOnly: true, maxAge: 345600000 } } )); app.use(cors({ origin: 'http://localhost:3000', credentials: true })); app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); next(); }); app.options('*', cors()) But the routes that I have setup is using axios.get in this express server.When I try to access a route in my React web app, then I get this error:localhost/:1 Access to XMLHttpRequest at 'http://authenticate.example.com/user' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. How can I fix this?As my react webapp is sending cookies on request, it works when I host it in localhost.

Submitted September 02, 2020 at 05:50PM by exilen

No comments:

Post a Comment