0

I have the below code:

var request = require('request');
var rp = require('request-promise');
module.exports = async function(context, req) {
    var arr = [[url1],[url2]]
    for (i = 0; i < arr.length; i++) {
        func(context, arr[i]);
    }
}
function func(context, urls) {
    const promises = urls.map(item => {
        return rp({
            uri: item,
            simple: false,
            resolveWithFullResponse: true
        }).then(response => {
            if (response.statusCode == 403) {

                var msg = "hello";
                return msg;
            } else {
                return null;
            }
        });
    });

    return Promise.all(promises).then(data => {
        // remove null results from array
        return data.filter(item => item !== null);
    });
}

And I keep getting this error. Sometimes it works, sometimes it doesn't. How can I fix this, and why does it work sometimes and sometimes not.

Result: Failure
Exception: RequestError: Error: connect ETIMEDOUT 

Do I need to inclue a pool option? Or perhaps a timeout? The error seems to be due to the size of arr.

JDT
  • 965
  • 2
  • 8
  • 20

1 Answers1

0

This is an issue on the network level and has most likely to do with your proxy configuration. And also you may get this error when requesting to a server using https (port 443) that didn't have a valid certificate. So, using the the following configuration could solve your problem.

return rp({
       uri: item,
       simple: false,
       resolveWithFullResponse: true,
       proxy: 'http://<your.proxy.info>:8080',
       strictSSL :false
})

Update:

No proxy, no streaming, just single request which starts several async request, setting this options:

agent: false, pool: {maxSockets: 8} 
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Thanks for the reply. Where would I find my proxy info to try this? – JDT Feb 19 '20 at 10:08
  • When do not have a valid certificate, you can confirm this behavior by editing your /etc/hosts (or hosts on Windows) adding a record with IP address and the host Common Name (that you can get check inside certificate) and then make the request again. – Joey Cai Feb 24 '20 at 01:02
  • Use `proxy:'http://username:password@host:port'`. For more, refer to this [article](https://learn.microsoft.com/en-us/azure/azure-functions/functions-proxies). – Joey Cai Feb 24 '20 at 01:03