Socket.IO connection blocked by CORS between your page and your Node.js server
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/socket.io/?EIO=4&transport=polling. (Reason: CORS header 'Access-Control-Allow-Origin' missing)Short answer
Since Socket.IO v3 the server sends no CORS headers until you set the cors option on the Server itself. Express middleware does not do it for you. List the exact page origin there, match credentials on the client, and check that the client really reaches the Socket.IO path, because a wrong host also shows as a CORS error.
Fix it fast
1Find out which cause you have
Ask the server for the handshake the way the browser would, with the page's origin. A healthy server answers with a body that starts with 0 and carries an Access-Control-Allow-Origin line.
curl -i "http://localhost:3000/socket.io/?EIO=4&transport=polling" \ -H "Origin: http://localhost:5173"2Match what you see, then jump to its fix
- 1
The curl answer has a body but no Access-Control-Allow-Origin headerThe Server was created without a cors optionFix - 2
The client sets withCredentials: true and the server answers with origin: '*'Cookies or credentials are used together with a wildcard originFix - 3
The curl in the first step returns an HTML page, a 404 or no answer, not a body starting with 0The client never reaches a Socket.IO server at allFix
- 1
What does the error look like?
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/socket.io/?EIO=4&transport=polling. (Reason: CORS header 'Access-Control-Allow-Origin' missing)Access to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=4&transport=polling' from origin 'http://localhost:5173' has been blocked by CORS policy
Cause 1 of 3
The Server was created without a cors option
1Check
Socket.IO v3 and later require CORS to be enabled explicitly on the Socket.IO server. Page and server on different ports or hosts are different origins, so the polling request is blocked. Note that WebSocket frames are not subject to CORS; the first HTTP long-polling request is what fails.
grep -n "new Server" -r src server.js 2>/dev/null2Fix
Pass the origin of the page in the cors option. The option is called origin, singular, and takes an array for several sites.
import { createServer } from 'node:http';
import { Server } from 'socket.io';
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: ['http://localhost:5173', 'https://app.example.com'],
},
});
httpServer.listen(3000);3Verify
Repeat the curl; the header must now be present and equal to the origin you sent.
curl -si "http://localhost:3000/socket.io/?EIO=4&transport=polling" \
-H "Origin: http://localhost:5173" | grep -i access-control-allow-originConfirmed by socket.io
Cause 2 of 3
Cookies or credentials are used together with a wildcard origin
1Check
The server options page states that origin '*' cannot be combined with credentials: true, because browsers block that pair. Look at both sides of the connection.
grep -n "withCredentials" -r src
grep -n "credentials" -r server.js src2Fix
Name the exact origin on the server, turn credentials on there, and keep withCredentials on the client.
// server
const io = new Server(httpServer, {
cors: { origin: ['https://app.example.com'], credentials: true },
});
// client
import { io } from 'socket.io-client';
const socket = io('https://api.example.com', { withCredentials: true });3Verify
The handshake response now carries Access-Control-Allow-Credentials: true.
curl -si "https://api.example.com/socket.io/?EIO=4&transport=polling" \
-H "Origin: https://app.example.com" | grep -i access-controlConfirmed by socket.io
Cause 3 of 3
The client never reaches a Socket.IO server at all
1Check
The troubleshooting guide says the same browser message appears when the client is not reaching the Socket.IO server at all. Client and server must also agree on the path option; the default is /socket.io/, and a namespace in the URL does not change it.
curl -i "http://localhost:3000/socket.io/?EIO=4&transport=polling"
grep -n "path" -r server.js src2Fix
Point the client at the host and port where the server listens, and give both sides the same path if you changed it.
const io = new Server(httpServer, { path: '/realtime/', cors: { origin: ['https://app.example.com'] } });
const socket = io('https://api.example.com', { path: '/realtime/' });3Verify
The handshake URL with your path returns a body starting with 0.
curl -s "https://api.example.com/realtime/?EIO=4&transport=polling"Confirmed by socket.io
How do I stop it coming back?
- Keep the allowed origins in one environment variable and use it for both the HTTP API and Socket.IO, so they cannot drift.
- Never ship origin: '*' on a server that also uses cookies.
- Add the curl handshake to your deploy checks, so a missing header shows up before users do.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Web apps
The browser blocks your API response: no Access-Control-Allow-Origin header
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.
4 causes, each with a fixBack end
The browser says io is not defined, or socket.io.js is not found
GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)
io is not defined means the Socket.IO client script never loaded.
3 causes, each with a fixBack end
Socket.IO answers 404 on the polling request
Cannot GET /socket.io/
A 404 on /socket.io/?EIO=4&transport=polling means the request is not reaching a Socket.IO server.
3 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fix