S
S
Stanislav2017-07-20 18:08:43
Node.js
Stanislav, 2017-07-20 18:08:43

How to terminate the process if the proxy server is unavailable?

It is necessary to somehow catch bad proxies and set timeout when requesting a URL
. I do it as follows

let options ={
    request: {
        uri: 'https://google.com/',
        json: true,
        timeout: 1500,
        proxy: 'https://ip:port/',
        headers: {
            'User-Agent': config.api.userAgent
        }
    }

};

request.get(options.request, (e, data) => {
    console.log(e);
    process.exit(0);
});

timeout does not work for some reason =(
If the proxy is bad, the script hangs and waits for a response. I need to make sure that if the URL is not reached within a few seconds, stop the script.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Serezha, 2017-07-20
@ms-dred

request.get('http://10.255.255.1', {timeout: 1500}, function(err) {
    console.log(err.code === 'ETIMEDOUT');
    // Set to `true` if the timeout was a connection timeout, `false` or
    // `undefined` otherwise.
    console.log(err.connect === true);
    process.exit(0);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question