title: REST API call using nodejs on localhost
body: <p>I have made a REST API using R language.  </p>
  
  <pre><code>#* @get /mean
  normalMean &lt;- function(samples=10){
    data &lt;- rnorm(samples)
    mean(data)
  }
  </code></pre>
  
  <p>I started the R server and tested the API using the url- <a href="http://localhost:8000/mean" rel="nofollow noreferrer">http://localhost:8000/mean</a> and it is working.</p>
  
  <p>However when I tried to invoke the API using nodejs it returns an error:</p>
  
  <pre><code>Error: socket hang up
  at TLSSocket.onHangUp (_tls_wrap.js:1124:19)
  at TLSSocket.g (events.js:292:16)
  at emitNone (events.js:91:20)
  at TLSSocket.emit (events.js:185:7)
  at endReadableNT (_stream_readable.js:974:12)
  at _combinedTickCallback (internal/process/next_tick.js:80:11)
  </code></pre>
  
  <p>Here is the nodejs code:</p>
  
  <pre><code>var https = require('https');
  var optionsget = {
  host : 'localhost', // here only the domain name
  // (no http/https !)
  port : 8000,
  path : '/mean', // the rest of the url with parameters if needed
  method : 'GET' // do GET
  };
  
  console.info('Options prepared:');
  console.info(optionsget);
  console.info('Do the GET call');
  
  var reqGet = https.request(optionsget, function(res) {
  console.log("statusCode: ", res.statusCode);
  // uncomment it for header details
  //  console.log("headers: ", res.headers);
  
  
  res.on('data', function(d) {
      console.info('GET result:\n');
      process.stdout.write(d);
      console.info('\n\nCall completed');
  });
  
  });
  </code></pre>
  
  <p>I am not understanding where I am going wrong. I intend to make a put request in a similar manner after this.</p>