Created
March 1, 2018 11:14
-
-
Save nacmacfeegle/f41e025b80daecb317884e341eb391af to your computer and use it in GitHub Desktop.
timeouts in http vs https client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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