Local API server failing with CORS error when connecting to CodeSandbox preview #8887
-
|
I am running a local Node.js backend API on my machine at localhost:5000 and trying to fetch data from it inside my frontend CodeSandbox project. The request fails with the console error: Access to fetch at 'http://localhost:5000/api' from origin 'https://xxxx.csb.app' has been blocked by CORS policy. How do I resolve this cross-origin issue in this environment? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({
origin: 'https://xxxx.csb.app', // Replace with your sandbox preview URL
credentials: true
}));CodeSandbox previews run over secure HTTPS ( To resolve this, configure your Express backend to allow the CodeSandbox origin:
|
Beta Was this translation helpful? Give feedback.
CodeSandbox previews run over secure HTTPS (
csb.app), while your local API runs on unsecure HTTP. The browser blocks this cross-origin request by default.To resolve this, configure your Express backend to allow the CodeSandbox origin:
npm install cors