nodejs httprequest with data - getting error getad

2019-01-11 00:59发布

Update - Answered by self

I see one has to make sure that the DNS is resolved properly from the machine, check out the node documentation to make sure that domain is resolvable.

Original Question

i am writing a nodes based program,in which the user can ask me to do a httprequest on their behalf {off course they provide me with some data, and method to call with} but every time i do a httprequest it gives me an error

getaddrinfo ENOENT this is how my code looks

function makehttprequest(deviceid, httpaction, httppath,methods, actiondata, callback) {
console.log('we are here with httpaction' + httpaction + ' path ' + httppath + ' method ' + methods + ' action data ' + actiondata);
 //do the http post work, get the data, and call the callback function with return data
 var options = {
   host: httpaction,
   port: 80,
   path: httppath,
   method: methods
 };

    try {
      var req = http.request(options, function(res) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
          console.log('BODY: ' + chunk);
        });
      });
    } catch(e) {
      console.log('error as : ' + e.message);
    }

    req.on('error', function(e) {
      console.log('problem with request: ' + e.message);
    });

    // write data to request body
    console.log('writing data to request ..');
    req.write(actiondata);
    console.log('finished writing data to request…');
    req.end();
    console.log('request ended…');
}

8条回答
倾城 Initia
2楼-- · 2019-01-11 01:07

I had a similar issue but running as a AWS Lambda function, so in case someone is having this issue with Lambda functions this is how I got it resolved.

  • Give your Lambda function a VPC.
  • Select at least 2 Subnets.
  • And select a Security Group.

I spent a day until I found this fix, hope it helps someone else.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-11 01:11

I hit this again today for a silly mistake. This was because port number was put as part of the hostname.

// wrong. gets error getaddrinfo ENOENT
var options = {
  hostName: 'localhost:1337',
  ....
}

// correct
var options = {
    hostname: 'localhost',
    port: 1337,
};
查看更多
Root(大扎)
4楼-- · 2019-01-11 01:13

The problem can also happen if you have a trailing slash:

Good: "www.google.com"

Bad: "www.google.com/"

查看更多
Rolldiameter
5楼-- · 2019-01-11 01:17

I've seen this happen when your host (which you pass in as httpaction) has the scheme (so "http://") in front of it. Your host should strictly be the domain like "www.google.com" not "http://www.google.com" or "www.google.com/hello-world" or "http://www.google.com/hello-world".

Keep it just the domain.

Here's an example: http://allampersandall.blogspot.com/2012/03/nodejs-http-request-example.html

查看更多
走好不送
6楼-- · 2019-01-11 01:18

Avoid all of these hostname/protocol/port/slash problems by using the request module instead of http

https://github.com/mikeal/request

查看更多
劳资没心,怎么记你
7楼-- · 2019-01-11 01:19

I was getting this error when calling server.listen(PORT, HOST); where HOST could not be resolved back to the local machine.

Once I changed this back to a hostname/domain name/ip that the local machine resolved to, this error went away.

Since I was trying to connect via a hostname for dev purposes I added an entry to my hosts file with the desired hostname and ensured that this matched the hostname passed to server.listen()

查看更多
登录 后发表回答