title: Error: ESOCKETTIMEDOUT but doesn't seem to be DNS like other SO posts suggest
body: <p>I am having some issues with the <code>npm request</code> package and getting it to call any URL properly unless the URL is to a server that is off. Example, I can't call <code>amazon.com</code> but I can call <code>dev.personalDomain.com</code> which points to an EC2 instance that is off entirely.</p>
  
  <p>I tried to strip out the application logic to make it so that just the error is produced.</p>
  
  <p>For this example assume <code>monitor.body.url = 'amazon.com'</code> is a string</p>
  
  <p><strong>My code</strong></p>
  
  <pre><code>request({url: `http://${monitor.body.url}`, timeout: 20}, function (error, response, body) {
      console.log(`Response: ${response}`);
      console.log(`Body: ${body}`);
      console.log(`Error: ${error}`);
      if(error == 'ENOTFOUND' || error == 'ETIMEDOUT') {
        response.statusCode = 500;
    }
    if(error || response.statusCode &lt; 200 &amp;&amp; response.statusCode &gt; 299 || !response.statusCode) {
      if(!response || !response.statusCode)  response.statusCode = 500;
  
      // Application logic
  
    }
    // If nothing went wrong just console.log -- This is used for debugging not useful in prod
    if(!error || response.statusCode &gt; 299 || response.statusCode &lt; 200)console.log(`Successfully called ${monitor.body.url}`);
  
  });
  </code></pre>
  
  <p><strong>Error message</strong></p>
  
  <pre><code>Response: undefined
  Body: undefined
  Error: Error: ESOCKETTIMEDOUT
  /home/ec2-user/environment/service/app.js:91
            if(!response || !response.statusCode)  response.statusCode = 500;
                                                                       ^
  
  TypeError: Cannot set property 'statusCode' of undefined
  </code></pre>
  
  <p>I went looking on google/SO and came across that it might be DNS causing the issue and to test/resolve that I use:</p>
  
  <pre><code>const dns = require("dns");
  dns.setServers(['8.8.8.8', '1.1.1.1']);
  </code></pre>
  
  <p>This made no change on the result of the request from above. </p>
  
  <p>Does anyone have a suggestion or idea on what this might be? I am having no luck finding an example that is close and produces the same error message. </p>
  
  <p>EDIT** </p>
  
  <p>My DNS is perfectly fine on my dev machine so zero issues there. Also I didn't have this issue when I had just <code>request({'http://${monitor.body.url}', function (error, response, body) {</code> so seems like the error with how I am using request </p>