Thursday, February 22, 2018

How to keep alive a connection behind proxy in node js

Leave a Comment

I am sending a https get request to an external api.

First I created the https agent as below:

import https from 'https';  const KeepAliveAgent = new https.Agent( {     keepAlive: true } ); 

then set the request options as below:

let options = {                 url: 'externalapiurl',                 method: "GET",                 qs: queryString,                 agent: KeepAliveAgent              }; 

I just mentioned sample strings for url and qs, in the original request I am using the actual api url and querystring, then I am sending the request as below:

            console.time( "requestTime" );             request( options, ( err, response, body ) => {                 if ( err ) {                     logger.warn( err.message );                 }                 console.timeEnd( "requestTime" ); }); 

This is working fine, but I am printing the time taken for response above and this time is much more when I send the request behind a proxy, when I doesn't use proxy it's taking less than half second but with proxy it's taking around 3 secs, so it seems the "keep alive" is not working behind the proxy, how to make this work? I tried same request using https-proxy-agent node module, but still the issue persists, appreciate any help.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment