Get your own Node server
const http = require('http');
const { once } = require('events');

// Create a simple HTTP server for testing
const server = http.createServer((req, res) => {
  // Simple response
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from test server!');
});

// Helper function to make a request and return a promise
function makeRequest(url) {
  return new Promise((resolve, reject) => {
    const req = http.get(url, (res) => {
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      res.on('end', () => {
        resolve({
          statusCode: res.statusCode,
          headers: res.headers,
          body: data
        });
      });
    });
    
    req.on('error', (err) => {
      reject(err);
    });
    
    req.end();
  });
}

// Main function to demonstrate the default agent
async function runDemo() {
  // Start the test server
  await new Promise((resolve) => {
    server.listen(0, '127.0.0.1', resolve);
  });
  
  const serverAddress = server.address();
  const baseUrl = `http://${serverAddress.address}:${serverAddress.port}`;
  
  console.log(`Test server running at ${baseUrl}`);
  console.log('Using the default global agent (http.globalAgent)');
  
  // Make a request using the default agent
  console.log('\n=== Making a request with the default agent ===');
  const response1 = await makeRequest(baseUrl);
  
  console.log('Response status code:', response1.statusCode);
  console.log('Response body:', response1.body.trim());
  
  // Show agent information
  console.log('\n=== Agent Information ===');
  console.log('Agent maxSockets:', http.globalAgent.maxSockets);
  console.log('Agent keepAlive:', http.globalAgent.keepAlive);
  console.log('Current active sockets:', Object.keys(http.globalAgent.sockets).length);
  console.log('Current free sockets:', Object.keys(http.globalAgent.freeSockets).length);
  
  // Make another request to show socket reuse
  console.log('\n=== Making another request to demonstrate socket reuse ===');
  const response2 = await makeRequest(baseUrl);
  
  console.log('Response status code:', response2.statusCode);
  console.log('Current active sockets:', Object.keys(http.globalAgent.sockets).length);
  console.log('Current free sockets:', Object.keys(http.globalAgent.freeSockets).length);
  
  // Close the server
  server.close();
  
  console.log('\n=== Demo complete ===');
  console.log('The test server has been stopped.');
  console.log('This is the end of the demo.');
}

// Run the demo and handle any errors
runDemo().catch((err) => {
  console.error('Error in demo:', err);
  server.close();
  process.exit(1);
});

              
Test server running at http://127.0.0.1:12345
Using the default global agent (http.globalAgent)

=== Making a request with the default agent ===
Response status code: 200
Response body: Hello from test server!

=== Agent Information ===
Agent maxSockets: Infinity
Agent keepAlive: false
Current active sockets: 0
Current free sockets: 0

=== Making another request to demonstrate socket reuse ===
Response status code: 200
Current active sockets: 0
Current free sockets: 0

=== Demo complete ===
The test server has been stopped.
This is the end of the demo.