Answer the question
In order to leave comments, you need to log in
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');
}
});
Answer the question
In order to leave comments, you need to log in
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');
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question