My Node.js setup involves the following flow:
Client --> Node.js --> External Rest API
The reverse response flow is required. To meet this requirement, I am tasked with capturing response headers from the External Rest API
and appending them to Node.js's response header in order to send a single response header back to the client. This is how I attempted it (from the Node.js side):
var resHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type,Last-modified,Connection,Access-Control-Allow-Origin,Access-Control-Allow-Methods,Date',
'Access-Control-Expose-Headers': 'Content-Type,Last-modified,Connection,Access-Control-Allow-Origin,Access-Control-Allow-Methods,Date'
};
I have stored the External API
's response header in a variable like so:
var curlHeadersResp; // Curl is used to retrieve response headers
My attempt to combine both responses looks like this:
var finalRespHeaders = new Array();
finalRespHeaders.push(curlHeadersResp);
finalRespHeaders.push(JSON.stringify(resHeaders));
res.writeHead(200, JSON.stringify(finalRespHeaders));
However, the resulting response header appears to be garbled:
HTTP/1.1 200 ["HTTP/1.1 200 OK\r\nServer: Apache-Coyote/1.1\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nDate: Mon, 03 Feb 2014 09:25:06 GMT\r\n\r\n","{\"Content-Type\":\"application/json\",\"Access-Control-Allow-Origin\":\"*\",\"Access-Control-Allow-Methods\":\"GET,PUT,POST,DELETE\",\"Access-Control-Allow-Headers\":\"Content-Type,Last-modified,Connection,Access-Control-Allow-Origin,Access-Control-Allow-Methods,Date\",\"Access-Control-Expose-Headers\":\"Content-Type,Last-modified,Connection,Access-Control-Allow-Origin,Access-Control-Allow-Methods,Date\"}"] Date: Mon, 03 Feb 2014 09:25:38 GMT
This seems to be an issue with concatenation. I would appreciate any insights or solutions on generating dynamic headers in this context.
Why am I doing this? Due to cross-domain request obstacles, I lack control over the external server. Therefore, I must append Allow cross domain headers from my Node.js side to enable cross-domain requests.