32

I train myslef with NodeJS and tried a simple GET call. Here is my code:

var http = require('http');

var options = {
    host: 'www.boardgamegeek.com',
    path: '/xmlapi/boardgame/1?stats=1',
    method: 'GET'
}

var request = http.request(options, function (response) {
    var str = ""
    response.on('data', function (data) {
        str += data;
    });
    response.on('end', function () {
        console.log(str);
    });
});

request.on('error', function (e) {
    console.log('Problem with request: ' + e.message);
});

request.end();

The URL called seems to work in my browser https://www.boardgamegeek.com/xmlapi/boardgame/1?stats=1

Anyway, I've got Problem with request: connect ETIMEDOUT when I run the code and I have no idea how to fix it.

What could cause this error ? Is it my code or is it a network/proxy issue?

2
  • 3
    Are you behind a proxy?
    – sdabet
    Oct 26, 2015 at 16:49
  • 1
    Yeah indeed, I've got a corporate proxy!
    – Alex
    Oct 26, 2015 at 16:54

10 Answers 10

21

When behind a proxy you need to make the following modifications (as explained in this answer):

  • put the proxy host in the host parameter
  • put the proxy port in the port parameter
  • put the full destination URL in the path parameter :

Which gives:

var options = {
    host: '<PROXY_HOST>',
    port: '<PROXY_PORT>',
    path: 'http://www.boardgamegeek.com/xmlapi/boardgame/1?stats=1',
    method: 'GET',
    headers: {
        Host: 'www.boardgamegeek.com'
    }
}
3
  • Thanks! It works. I had to add http in the path and an host header but not it's working trough the proxy.
    – Alex
    Oct 27, 2015 at 8:40
  • I have an username and a password on my proxy, how can I implement this? Jan 15, 2019 at 18:23
  • What about if the code comes from a 3pp and we cannot modify it? how to bypass the proxy? Aug 3, 2021 at 9:06
4

In my case it was because of http but not https as required

3

If this error occurs while using POSTMAN.

So, when you call a request in Postman and the API requires your VPN to be up before you can make a successful call. You will need to connect or reconnect your VPN.

In my case, I was working on a company's laptop. I found out it was the VPN that was down.

1

I was facing this issue on Ubuntu Server while maintaining a node instance on PM2. Basically after restarting the instance after taking the pull I was getting the same error on initial connection of mySQL inside the code.

Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/home/deam_server/node_modules/mysql/lib/Connection.js:419:13)
at Object.onceWrapper (events.js:275:13)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:442:20)
at Socket._onTimeout (net.js:447:8)
at ontimeout (timers.js:427:11)
at tryOnTimeout (timers.js:289:5)
at listOnTimeout (timers.js:252:5)
at Timer.processTimers (timers.js:212:10)

Though the same code was running perfectly on my local machine. After that I used "df" command which helped me to realise that my root directory was 100% occupied. I allotted some memory to the root directory and the restarted the node instance and then it started working fine.

1

The following change with the request worked for me:

 var options = {
         proxy:'PROXY URL', 
         uri: 'API URL',
         method: 'GET' 
         }
 request(options, function (err, response, body) {
     if(err){
        console.log('error:', err);
       } else {
     console.log('body:', body);
      }
   });
1

In my case it was a misconfigured subnet. Only one of the 2 subnets in the ELB worked. But my client kept trying to connect to the misconfigured one.

1

if you have URL like :

  URL: 'localhost:3030/integration', 

The URL above cause some issues because HTTP does not exist at the beginning of URL so Just change it to it should work.

URL: 'http://localhost:3030/integration', 
  
1

In my case, I was getting this error because the request body of my post api was very large.

0

Sometimes it can simply be because your internet is down.

0

A reason that I encountered was that the TLS certificate on the server was not valid, for the https request that I was making.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.