0

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.

2
  • According to Node.js http Docs, you should pass headers as an object, while you're passing a JSON string. Commented Feb 3, 2014 at 9:37
  • @LeonidBeschastny, yes I tried that, but just I am getting as [object Object] Commented Feb 3, 2014 at 9:51

1 Answer 1

0

From Node.JS Documentation:

//response.writeHead(statusCode, [reasonPhrase], [headers])
//                    number      string          object

var body = 'hello world';
response.writeHead(200, {
  'Content-Length': body.length,
  'Content-Type': 'text/plain' });

You don't need to create an array an push the objects to it, join together the curlHeadersResp and resHeaders:

Object.keys(curlHeadersResp).forEach(function (element) {
    resHeaders[element] = curlHeadersResp[element;
});

Where you write the header you don't need to stringify the object, just:

res.writeHead(200, resHeaders);
Sign up to request clarification or add additional context in comments.

5 Comments

Yes I tried that, but just I am getting as [object Object]
Updated answer, check it out, you need to have in the end an Object! NOT a string or an array
Ok, thanks I tried this, but now I am getting cannot call keys on non-object error. I think it's because curlHeadersRespI am getting in json string.
Yes, this may be the problem, parse it and try again
Yes I tried, now I am getting these type of error, unexpected token H date=Mon Feb 03 2014 10:07:35 GMT+0000 (UTC)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.