title: http request from node.js local server port 5000 to 8000 gives error
body: <p>I am trying to make a GET request from localhost:8000 to localhost:5000 for the url '/serverTest'. I get socket hang up error. How do I fix this?</p>
  
  <p>Server on port 5000 (server which will process the request):</p>
  
  <pre><code>var express = require("express"), http = require('http');
  app.get('/serverTest', function(req, res){
      //authenticate request
      //send message
      res.writeHead(200, {'Content-Type':'application/json'});
      res.end(JSON.stringify({message:'hello'}));
  }); 
  app.listen(5000, function() {
      console.log("Listening on " + port);
  });
  </code></pre>
  
  <p>Server on port 8000 (server which makes the request):</p>
  
  <pre><code>var express = require("express"), http = require('http');
  var port = 8000;
  app.listen(port, function() {
      console.log("Listening on " + port);
      makeACall();
  });
  
  
  function makeACall(){
      var options = {
          host:'localhost',
          port: 5000,
          path: '/serverTest',
          method: 'GET'
      };
  
      http.request(options, function(response) {
          var str = '';
          response.on('data', function (chunk) {
              str += chunk;
          });
  
          response.on('end', function () {
              console.log(str);
          });
      });
  }
  </code></pre>
  
  <p>Error obtained by the server hosted at port 8000:</p>
  
  <pre><code>events.js:72
  throw er; // Unhandled 'error' event
  ^
  Error: socket hang up
  at createHangUpError (http.js:1442:15)
  at Socket.socketOnEnd [as onend] (http.js:1538:23)
  at Socket.g (events.js:175:14)
  at Socket.EventEmitter.emit (events.js:117:20)
  at _stream_readable.js:910:16
  at process._tickCallback (node.js:415:13)
  </code></pre>