S
S
Sokolov092015-06-18 01:42:39
Node.js
Sokolov09, 2015-06-18 01:42:39

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

1 answer(s)
T
Timur Shemsedinov, 2015-06-18
@Sokolov09

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': '*'
});

But if it doesn’t work, then make a parameter in the SSE url of the handler https://[url-server]:8081/event?domain=[uri-client] and substitute not *, but for each your own domain.
In addition, try not to hang on 8081, but everything on 80, browsers, firewalls and different gateways love when everything is on 80. Yes, and then it will be easier to balance, hardware balancers and specialized balancing services love 80.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question