Answer the question
In order to leave comments, you need to log in
How to create a cross domain request in nodejs?
The essence of the problem:
there is a group of sites: which connect to the server on nodejs and listen.
As soon as the event is triggered on the server, a broadcast is sent.
What's the problem:
EventSource cannot load https://[url-server]:8081/event. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin ' https://[url-client] ' is therefore not allowed access.
This works but only for 1 domain:
'GET /event': function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no -cache',
'Access-Control-Allow-Origin': ' https://[uri-client] ',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Methods': 'GET,PUT ,POST,DELETE',
"Access-Control-Allow-Headers": "Origin,X-Requested-With, Content-Type, Accept"
});
request.socket.setTimeout(3000 * 60 * 60);
request.on('close', function () {
Clients.remove(response);
});
clients.add(response);
},
Searching the Internet, I found a solution: you need to specify
'Access-Control-Allow-Origin': '*',
'GET /event': function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Access-Control -Allow-Origin': '*',
});
request.socket.setTimeout(3000 * 60 * 60);
request.on('close', function () {
Clients.remove(response);
});
clients.add(response);
},
Tried adding
'Access-Control-Allow-Credentials': 'false',
and
'Access-Control-Allow-Credentials': null,
I think maybe the ssl connection is to blame?
Or are some headers missing?
Answer the question
In order to leave comments, you need to log in
In general, it worked like this all my life:
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=UTF-8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': '*'
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question