https_agent.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. const https = require('https');
  3. const HttpsAgent = require('..').HttpsAgent;
  4. const keepaliveAgent = new HttpsAgent({
  5. keepAlive: true,
  6. });
  7. // https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8
  8. const options = {
  9. host: 'github.com',
  10. port: 443,
  11. path: '/',
  12. method: 'GET',
  13. agent: keepaliveAgent,
  14. };
  15. let start = Date.now();
  16. const req = https.request(options, res => {
  17. console.log('STATUS1: %d, %d ms', res.statusCode, Date.now() - start);
  18. console.log('HEADERS1: %j', res.headers);
  19. res.setEncoding('utf8');
  20. res.on('data', chunk => {
  21. console.log('BODY1: %d', chunk.length);
  22. });
  23. res.on('end', () => {
  24. process.nextTick(() => {
  25. start = Date.now();
  26. https.get(options, res => {
  27. console.log('STATUS2: %d, %d ms', res.statusCode, Date.now() - start);
  28. console.log('HEADERS2: %j', res.headers);
  29. res.setEncoding('utf8');
  30. res.on('data', chunk => {
  31. console.log('BODY2: %d', chunk.length);
  32. });
  33. });
  34. });
  35. });
  36. });
  37. req.on('error', e => {
  38. console.log('problem with request: ' + e.message);
  39. });
  40. req.end();
  41. setTimeout(() => {
  42. console.log('keep alive sockets:', keepaliveAgent);
  43. process.exit();
  44. }, 5000);