How to set headers value in node js coming from respose (server side)?

Hi All,

I have implement concurrent/parallel calls to multiple rest APIs(backend) using JavaScript and have merged the response from the all the backend APIs.

My question is how to set the headers value that is coming from response(server side). Below is my code. Please suggest the edits.

var http = require('http');
var async = require('async');
var request = require('request');
var url = require('url');
var querystring = require('querystring');
var _under = require("underscore");
var apigee = require('apigee-access');
var svr = http.createServer(function (req, resp) {
      var q = url.parse(req.url, true);
      var headersValue = req.headers;
      var urlRequest = apigee.getVariable(req, 'someServerIp'); console.log(urlRequest);
      async.parallel({
        one: function (callback)
        request({ headers: headersValue,
                  uri: urlRequest + q.path,
                  method: 'Get' },
                function (error, response, body) {
                  if (!error && response.statusCode == 200) {
                    console.log('Body');
                    callback(null, JSON.parse(body)); }
                  else {
                    callback(response.statusCode, JSON.parse(body));
                  }
                });
      },
                     two: function (callback) {
                       request({
                         headers: headersValue,
                         uri: urlRequest + q.pathname + '/schools' + '?' + querystring.stringify(q.query), method: 'Get' },
                               function (error, response, body)
                               { if (!error && response.statusCode == 200) {
                                 callback(null, JSON.parse(body));
                               } else {
                                 callback(response.statusCode, JSON.parse(body));
                               }
                               });
                     }
                    },
      function (statusCode, results, response) {
        if (!statusCode) {
          resp.writeHead(200, results.one.headersValue);
          a = results.one;
          b = results.two;
        } else {
          resp.writeHead(statusCode, results.one.header);
          c = results.one;
          d = results.two;
        }
        if (!!a && !!b) {
          results = _under.extend(a, b);
        } else {
          results = _under.extend(c, d);
        }
        resp.end(JSON.stringify(results));
      });
                           });
svr.listen(9000, function () {
  console.log('Listening');
});


Solved Solved
0 2 25.2K
1 ACCEPTED SOLUTION

look here.

In your anonymous function, you need to do

resp.setHeader('Content-Type', 'application/json');

...or whatever, to set a header. Do it before calling

resp.end(JSON.stringify(results));

Also, replace writeHead() with the appropriate calls to set statusCode. Calling writeHead() disallows you from setting further headers. see here. your code should look like this:

function (statusCode, results, response) {
        if (!statusCode) {
          resp.statusCode=200;
          resp.setHeader('myheader','my-header-value');
          // add other headers here
          a = results.one;
          b = results.two;
        } else {
          resp.statusCode = statusCode;
          resp.setHeader('myheader','my-header-value');
          // add other headers here
          c = results.one;
          d = results.two;
        }
        if (!!a && !!b) {
          results = _under.extend(a, b);
        } else {
          results = _under.extend(c, d);
        }
        resp.end(JSON.stringify(results));
      });

Alternatively you can use writeHead() ONCE, and when doing so, pass ALL headers you want to write to the response.

function (statusCode, results, response) {
        if (!statusCode) {
          resp.writeHead(200, { 'header1' : 'value1', header2: 'value2'});
          a = results.one;
          b = results.two;
        } else {
          resp.writeHead(statusCode, { 'header1' : 'value1', header2: 'value2'});
          c = results.one;
          d = results.two;
        }
        if (!!a && !!b) {
          results = _under.extend(a, b);
        } else {
          results = _under.extend(c, d);
        }
        resp.end(JSON.stringify(results));
      });

This is a nodejs thing only. Neither your question nor this answer has anything to do with Apigee Edge.

View solution in original post

2 REPLIES 2

look here.

In your anonymous function, you need to do

resp.setHeader('Content-Type', 'application/json');

...or whatever, to set a header. Do it before calling

resp.end(JSON.stringify(results));

Also, replace writeHead() with the appropriate calls to set statusCode. Calling writeHead() disallows you from setting further headers. see here. your code should look like this:

function (statusCode, results, response) {
        if (!statusCode) {
          resp.statusCode=200;
          resp.setHeader('myheader','my-header-value');
          // add other headers here
          a = results.one;
          b = results.two;
        } else {
          resp.statusCode = statusCode;
          resp.setHeader('myheader','my-header-value');
          // add other headers here
          c = results.one;
          d = results.two;
        }
        if (!!a && !!b) {
          results = _under.extend(a, b);
        } else {
          results = _under.extend(c, d);
        }
        resp.end(JSON.stringify(results));
      });

Alternatively you can use writeHead() ONCE, and when doing so, pass ALL headers you want to write to the response.

function (statusCode, results, response) {
        if (!statusCode) {
          resp.writeHead(200, { 'header1' : 'value1', header2: 'value2'});
          a = results.one;
          b = results.two;
        } else {
          resp.writeHead(statusCode, { 'header1' : 'value1', header2: 'value2'});
          c = results.one;
          d = results.two;
        }
        if (!!a && !!b) {
          results = _under.extend(a, b);
        } else {
          results = _under.extend(c, d);
        }
        resp.end(JSON.stringify(results));
      });

This is a nodejs thing only. Neither your question nor this answer has anything to do with Apigee Edge.

@Dino-at-GoogleThanks for the information.

If Im not wrong response.headers contains all the information about the headers values. Can I use it in the writeHead() , adding all the response headers value in one shot?