我已经创建了一个部署在Heroku上的Web应用程序以及一个单独的节点服务器,该服务器对该应用程序进行API调用。当服务器发出请求时,出现ENOTFOUND错误。

服务器请求:

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 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)


但是,我尝试使用cURL成功发布相同的JSON正文,因此可以在此实例中找到该地址:

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


我在Node中提出的请求不正确吗?
(注意:应用程序的URL已更改为匿名)

最佳答案

gameOptions更改为此

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

10-07 21:05