1

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) + '"' }
};
Michael Andrews
  • 194
  • 6
  • 16
  • Regarding your edit, it should be `host: '192.168.1.59'` not `hostname: 'http://192.168.1.59'`. – mscdex Sep 30 '14 at 19:08

1 Answers1

2

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.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I had actually thought about that and tried it. Switching to this still 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) + '"' } }; – Michael Andrews Sep 30 '14 at 19:06
  • I think that was it as it's just hanging there now like it's running. Thanks! – Michael Andrews Sep 30 '14 at 21:16