Skip to content
Discussion options

You must be logged in to vote

You need to enable CORS (Cross-Origin Resource Sharing) so your frontend (port 3000) can talk to your backend (port 4000).

Install the cors middleware:
Then update your server:

const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors()); // allow all origins

app.get('/api/data', (req, res) => res.json({ message: 'Hello' }));
app.listen(4000);

If you want to limit it:

app.use(cors({ origin: 'http://localhost:3000' }));

That fixes the CORS error.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@steven8306
Comment options

@ai-28
Comment options

Answer selected by steven8306
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants