title: Any way to make a generic response handler in nodejs?
body: <p>I'm trying to write a couple of endpoints that will make GET and POST http requests to various backend services, the format of data is all going to be very similar so responseHandler function will be copied over and over to different route function, I wonder if there is a way to externalize responseHandler for reuse. I tried to just move it out, but then I would lose reference to res. Anyone has any tips on a more modular design?</p>
  
  <pre><code>routes['/endpoint'] = function(req, res){
      console.log("Serving endpoint: /endpoint")
      var params={"param": "param-value"}
  
      var options = {
        host: 'localhost',
        path: '/service?param='+params.param,
        method: 'GET'
      };
  
      var responseHandler = function(response) {
        var data = '';
  
        // keep track of the data you receive
        response.on('data', function(chunk) {
          data += chunk + "\n";
        });
  
        // finished? ok, send the data to the client in JSON format
        response.on('end', function() {
              res.header("Content-Type:","application/json");
              res.end(data);
        });
      };
  
      // make the request, and then end it, to close the connection
      http.request(options, responseHandler).end();
  };
  </code></pre>