Skip to content

Instantly share code, notes, and snippets.

@nacmacfeegle
Created March 1, 2018 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nacmacfeegle/f41e025b80daecb317884e341eb391af to your computer and use it in GitHub Desktop.
Save nacmacfeegle/f41e025b80daecb317884e341eb391af to your computer and use it in GitHub Desktop.
timeouts in http vs https client
const httpsClient = require('https');
const httpClient = require('http');
const opts = {
hostname: 'httpbin.org',
method: 'GET',
path: '/delay/3',
timeout: 1000
};
const httpsOpts = Object.assign({port: 443}, opts);
const httpOpts = Object.assign({port: 80}, opts);
const makeRequest = (client, opts) => {
console.log(`making request to ${client.globalAgent.protocol}//${opts.hostname}${opts.path} on port ${opts.port}`);
const req = client.request(opts, (res) => {
console.log(`response from ${client.globalAgent.protocol}//${opts.hostname}${opts.path}`, res.statusCode);
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.end();
req.on('error', (error) => {
console.error('client.request error', error);
});
req.on('timeout', () => {
console.log(`request to ${client.globalAgent.protocol}//${opts.hostname}${opts.path} timeout! + (${opts.timeout / 1000}) seconds expired`);
req.destroy();
});
};
makeRequest(httpsClient, httpsOpts);
makeRequest(httpClient, httpOpts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment