Get your own Node server
const http = require('http');

// Create a simple HTTP server to respond to our test request
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from the server!');
});

// Start the test server
server.listen(0, 'localhost', () => {
  const serverPort = server.address().port;
  console.log(`Test server running at http://localhost:${serverPort}`);

  // Make an HTTP request to our test server
  const req = http.request({
    hostname: 'localhost',
    port: serverPort,
    path: '/test?q=nodejs',
    method: 'GET',
    headers: {
      'User-Agent': 'Node.js Demo Client',
      'Accept': 'text/plain',
      'X-Custom-Header': 'CustomValue'
    }
  }, (res) => {
    // 'res' is the IncomingMessage object (response)
    console.log('=== Response Received ===');
    
    // Basic message properties
    console.log('Status Code:', res.statusCode);
    console.log('Status Message:', res.statusMessage);
    console.log('HTTP Version:', res.httpVersion);
    
    // Headers
    console.log('\nHeaders:');
    console.log(JSON.stringify(res.headers, null, 2));
    
    // Raw headers
    console.log('\nRaw Headers:');
    console.log(res.rawHeaders);
    
    // Socket information
    console.log('\nSocket Information:');
    console.log('Remote Address:', res.socket.remoteAddress);
    console.log('Remote Port:', res.socket.remotePort);
    
    // Reading the message body
    let body = [];
    
    // Data events emit when chunks of the body are received
    res.on('data', (chunk) => {
      body.push(chunk);
      console.log(`\nReceived chunk of ${chunk.length} bytes`);
    });
    
    // End event is emitted when the entire body has been received
    res.on('end', () => {
      const responseBody = Buffer.concat(body).toString();
      console.log('\n=== Response Body ===');
      console.log(responseBody);
      
      // Close the test server
      server.close(() => {
        console.log('\nTest server closed');
      });
    });
    
    // Handle response errors
    res.on('error', (err) => {
      console.error('Response error:', err);
      server.close();
    });
  });
  
  // Handle request errors
  req.on('error', (err) => {
    console.error('Request error:', err);
    server.close();
  });
  
  // End the request (no body for this GET request)
  req.end();
});

              
Test server running at http://localhost:12345
=== Response Received ===
Status Code: 200
Status Message: OK
HTTP Version: 1.1

Headers:
{
  "content-type": "text/plain",
  "date": "Tue, 18 Jun 2025 06:37:21 GMT",
  "connection": "close",
  "transfer-encoding": "chunked"
}

Raw Headers:
[
  'Content-Type',
  'text/plain',
  'Date',
  'Tue, 18 Jun 2025 06:37:21 GMT',
  'Connection',
  'close',
  'Transfer-Encoding',
  'chunked'
]

Socket Information:
Remote Address: ::1
Remote Port: 12345

Received chunk of 21 bytes

=== Response Body ===
Hello from the server!

Test server closed