Ben
Ben

Reputation: 777

Node.js Web App API call results in ENOTFOUND

I've created a web app deployed on Heroku and a separate node server which makes API calls to the app. When the server makes a request I get an ENOTFOUND error.

Server request:

var gameOptions, body, gameReq;
var currentOptions, currReq;

// Create New Game --------------------------------------------------

// I have also tried hostname: "https://myChessApp.herokuapp.com/api"
gameOptions = {
    hostname: 'myChessApp.herokuapp.com/api',
    path: '/games',
    headers: {'Content-Type' : 'application/json'},
    method: 'POST'
};

body = {
    fen :"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
    pgn :"",
    createdAt : Date.now(),
    lastMoveAt : Date.now()
}

gameReq = HTTPS.request(gameOptions, function(res) {
        if(res.statusCode == 202) {
            console.log('Game Created: ' + res.text);
        } else {
            console.log('Bad status code: ' + res.statusCode);
        }
});

gameReq.on('error', function(err) {
    console.log('error creating game '  + JSON.stringify(err));
    console.error(err.stack);
});

gameReq.on('timeout', function(err) {
    console.log('timeout creating game '  + JSON.stringify(err));
});

gameReq.end(JSON.stringify(body));

Error:

error creating game {"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo","hostname":"myChessApp.herokuapp.com/api","host":"myChessApp.herokuapp.com/api","port":443}

Error: getaddrinfo ENOTFOUND myChessApp.herokuapp.com/api myChessApp.herokuapp.com/api:443
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

However, I have tried posting the same JSON body using cURL successfully, so the address can be found in this instance:

curl -X POST -H "Content-Type: application/json" -d '{
    "fen":"fen",
    "pgn":"",
    "createdAt":1,
    "lastMoveAt":2
}' "https://myChessApp.herokuapp.com/api/games"

Am I making my request incorrectly in Node? (Note: the app URL has been changed for anonymity)

Upvotes: 1

Views: 1497

Answers (1)

Michał Młoźniak
Michał Młoźniak

Reputation: 5556

Change gameOptions to this

gameOptions = {
    hostname: 'myChessApp.herokuapp.com',
    path: '/api/games',
    headers: {'Content-Type' : 'application/json'},
    method: 'POST'
};

Upvotes: 1

Related Questions