Error: getaddrinfo ENOTFOUND while making get request to localhost, Nodejs

3.4k Views Asked by At

I am trying to make a het request to etcd instance running in my local trough the node http module.

the code look like this

'use strict';
const express = require('express');
const app = express();
var http = require('http');

const port = 10111;

var encoded_url = encodeURI('/v2/keys/message -X GET');

var options = {
  host: 'http://127.0.0.1:2379',
  path: encoded_url
};

var callback = function (response) {
  var str = '';

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });

  //the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    console.log(str);
  });
}

http.request(options, callback).end();

app.listen(port, () => {
  console.log("server started on port " + port);
});

but I am getting the following error

Error: getaddrinfo ENOTFOUND http://127.0.0.1:2379 http://127.0.0.1:2379:80
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

If I make the same curl request from the terminal I get the result

curl http://127.0.0.1:2379/v2/keys/message -X GET

not able to figure out what is the issue.

1

There are 1 best solutions below

0
On BEST ANSWER

By default http.request() use port 80.

Use this instead:

var options = {
  protocol: 'http:',
  host: '127.0.0.1',
  port: 2379,
  path: encoded_url
};