getaddrinfo ENOTFOUND with Node.js http.request()

7.8k Views Asked by At

I'm trying to follow the badgekit tutorial here https://github.com/mozilla/badgekit-api/wiki/Using-BadgeKit-API#references

However, the error I'm getting is a node error with my syntax (I think). This is what I have in the file RetrieveBadgeData.js

var http = require("http");
var jws = require('jws');

var claimData = {
    header: { typ: 'JWT', alg: 'HS256' },
    payload: {
        key: 'mastersecret',
        exp: Date.now() + (1000 * 60),
        method: 'GET',
        path: '/systems'
    },
    secret: 'mastersecret'
};
var requestOptions = {
    host: 'http://192.168.1.59:8080',
    path: '/systems',
    method: 'GET',
    headers: { 'Authorization': 'JWT token="' + jws.sign(claimData) + '"' }
};

var apiRequest = http.request(requestOptions, function (apiResult) {
    apiResult.on('data', function (badgeData) {
        //process badgeData

    });
});

If I browse to 192.168.1.59:8080 I get what you're supposed to get back if the node API is running But, when I run node RetrieveBadgeData.js (executing the code above) I get this error.

events.js:72
    throw er; // unhandled 'error' event
          ^
Error: getaddrinfo ENOTFOUND
    at errnoException (dns.js:37:11)
    at Object.onanswer [as oncomplete] (dns.js:124:16)

Any ideas?

Edit I've also tried this, which gives me the same error

var requestOptions = {
    hostname: 'http://192.168.1.59'
    , port: 8080
    , path: '/systems/badgekit/badges'
    , method: 'GET'
    , headers: { 'Authorization': 'JWT token="' + jws.sign(claimData) + '"' }
};
1

There are 1 best solutions below

2
On BEST ANSWER

The problem is how you're specifying the host:

host: 'http://192.168.1.59:8080'

Use this instead:

hostname: '192.168.1.59`,
port: 8080

The http:// is the protocol, not the host. The error you're getting is that Node.js can't resolve the hostname you provided. The Node.js docs also suggest that the usage of hostname is preferred over host.