T
T
thisuser2018-12-14 16:20:54
Node.js
thisuser, 2018-12-14 16:20:54

How to catch in the code that the IP is blocked?

Good afternoon..
In the process of implementing a simple task on nodejs , I encountered the fact that sometimes I don’t get any response from the server when I request via request .
I came to the conclusion that this could happen if the ip of my server got banned. (Maybe a wrong conclusion, but likely, since ips change and they are not very "quality" sometimes).
Here is an example of a simple query:

var options = {
  url: url,
  encoding: null
};

request.get(options, function (err, res) {
// сюда не попадает уже.. ;) виснет на запросе.
  if (err) {
      console.log('не ok');
  } else {
      console.log('ok');
  }
});

It is clear that in cases of a response like 200, everything is fine .. - otherwise an error.
But in my case, when accessing some IPs, nothing happens. How can this be better captured?
Thanks to.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2018-12-14
@thisuser

Set a timeout for the request. If the answer did not come before the timeout expires, then “not ok”
I don’t know the capabilities of the request module and I can’t look normally, it’s not convenient from the phone, so I can only offer a universal, but not very ideologically correct solution:

var options = {
  url: url,
  encoding: null,
  timeout: 60*1000
};

function req(opts, cb){
  var isTimeout = false;
  setTimeout( ()=>{
     if( isTimeout) return;
     isTimeout = true;
     cb("time is out", {});
  }, opts.timeout);
  request.get(opts, function (err, res) {
  // сюда не попадает уже.. ;) виснет на запросе.
    if( isTimeout ) return;
    isTimeout = true;
    cb(err, res);
  });
)

// выполняем запрос с таймаутом в 60 сек.
req(options, function(err, res){
    if (err) {
        console.log('не ok');
    } else {
        console.log('ok');
    }
});

PS: the correct solution would be to do as it was done in your example, but adding a timeout value to options. Look in the request module's off-docs for how to do this.

T
thisuser, 2018-12-14
@thisuser

Thank you .. yes, I found about the timeout in request - it's the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question