const http = require('http');
const https = require('https');
// Get the default global agents
const defaultHttpAgent = http.globalAgent;
const defaultHttpsAgent = https.globalAgent;
// Create a custom HTTP agent
const customHttpAgent = new http.Agent({
keepAlive: true,
maxSockets: 25,
keepAliveMsecs: 10000, // Keep sockets around for 10 seconds (default: 1000)
maxFreeSockets: 10, // Maximum number of free sockets to keep open
timeout: 60000 // Socket timeout in milliseconds
});
// Create a custom HTTPS agent
const customHttpsAgent = new https.Agent({
keepAlive: true,
maxSockets: 25,
rejectUnauthorized: true, // Reject unauthorized TLS certificates
// Additional TLS options can be specified here
// e.g., key, cert, ca, etc.
});
// Log agent configurations
console.log('=== Default HTTP Agent ===');
console.log('maxSockets:', defaultHttpAgent.maxSockets);
console.log('maxFreeSockets:', defaultHttpAgent.maxFreeSockets);
console.log('keepAlive:', defaultHttpAgent.keepAlive);
console.log('\n=== Custom HTTP Agent ===');
console.log('maxSockets:', customHttpAgent.maxSockets);
console.log('maxFreeSockets:', customHttpAgent.maxFreeSockets);
console.log('keepAliveMsecs:', customHttpAgent.keepAliveMsecs);
console.log('timeout:', customHttpAgent.timeout);
console.log('\n=== Default HTTPS Agent ===');
console.log('maxSockets:', defaultHttpsAgent.maxSockets);
console.log('rejectUnauthorized:', defaultHttpsAgent.options.rejectUnauthorized);
// Example of using the custom agent with a request
console.log('\n=== Example: Using Custom Agent ===');
// Create a test server to demonstrate agent usage
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from the test server!');
});
// Start the server on a random port
server.listen(0, 'localhost', () => {
const port = server.address().port;
const url = `http://localhost:${port}`;
console.log(`\nTest server running at ${url}`);
// Make a request using the custom agent
const req = http.get({
hostname: 'localhost',
port: port,
path: '/',
agent: customHttpAgent // Use our custom agent
}, (res) => {
console.log(`\nResponse status: ${res.statusCode}`);
console.log('Response headers:', JSON.stringify(res.headers, null, 2));
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Response body:', data);
// Check agent stats
console.log('\nAgent stats after request:');
console.log('Free sockets:', Object.keys(customHttpAgent.freeSockets).length);
console.log('Active sockets:', Object.keys(customHttpAgent.sockets).length);
// Close the server and destroy the agent
server.close(() => {
customHttpAgent.destroy();
console.log('\nTest server closed and agent destroyed');
});
});
});
// Handle request errors
req.on('error', (err) => {
console.error('Request error:', err);
server.close();
customHttpAgent.destroy();
});
});
// Handle server errors
server.on('error', (err) => {
console.error('Server error:', err);
customHttpAgent.destroy();
});