My situation of using Node.js is like this:
Client --> Node.js --> External Rest API
So, vice-versa will be the response. As per requirement, I need to capture response headers from External Rest API and append it to Node.js's response header and to a client I need to send it as a single response header. To which I tried like this (this header is from 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 am getting External API's response header in a variable like this:
var curlHeadersResp; //I am executing curl to get response headers
Finally I am trying to combile both like this:
var finalRespHeaders = new Array();
finalRespHeaders.push(curlHeadersResp);
finalRespHeaders.push(JSON.stringify(resHeaders));
res.writeHead(200, JSON.stringify(finalRespHeaders));
But the response header I am getting is in junk like this:
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
I think it is a problem of concatination. My question is if anyone has any idea of generating dynamic headers or anyone could shed some light on this it would be very helpful.
Why I am doing like this? Because of cross-domain requests problems, so I don't have control on external server. So from my Node.js side I need to append Allow cross domain headers so that cross domain requests are possible.
[object Object]