本文介绍了“错误:getaddrinfo ENOTFOUND";发出HTTP请求时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是AWS Lambda函数中的代码:

here's the code in AWS Lambda function:

var https = require('https');
exports.handler = (event, context, callback) => {
    var params = {
        host: "bittrex.com",
        path: "/api/v1.1/public/getmarketsummaries"
    };
    var req = https.request(params, function(res) {
        var test = res.toString();
        console.log(JSON.parse(test));
        //console.log(JSON.parse(res.toString()));
    });
    req.end();
};

其他解决方案无效.

推荐答案

我修改了您的代码以使其在AWS Lambda Node.js 6.10中正常工作.我将Lambda超时设置为60秒以进行测试.

I modified your code to work correctly in AWS Lambda Node.js 6.10. I set the Lambda timeout to be 60 seconds for testing.

最大的变化是添加了"res.on('data',function(chunk){}:")和"res.on('end',function(){}").

The big change is adding "res.on('data', function(chunk) {}:" and "res.on('end', function() {}".

var https = require('https');
exports.handler = (event, context, callback) => {
    var params = {
        host: "bittrex.com",
        path: "/api/v1.1/public/getmarketsummaries"
    };
    var req = https.request(params, function(res) {
        let data = '';
        console.log('STATUS: ' + res.statusCode);
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            data += chunk;
        });
        res.on('end', function() {
            console.log("DONE");
            console.log(JSON.parse(data));
        });
    });
    req.end();
};

这篇关于“错误:getaddrinfo ENOTFOUND";发出HTTP请求时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 16:42