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

// Create a test server to demonstrate IncomingMessage
const server = http.createServer((req, res) => {
  // Log request details
  console.log('=== Incoming Request ===');
  console.log(`Method: ${req.method}`);
  console.log(`URL: ${req.url}`);
  console.log('Headers:', JSON.stringify(req.headers, null, 2));
  console.log('HTTP Version:', req.httpVersion);
  console.log('Raw Headers:', req.rawHeaders);
  
  // Log client connection info
  const clientAddress = req.socket.remoteAddress;
  const clientPort = req.socket.remotePort;
  console.log(`Client connected from ${clientAddress}:${clientPort}`);
  
  // Handle different request methods
  if (req.method === 'GET') {
    // For GET requests, send a simple response
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello from the server!');
  } 
  else if (req.method === 'POST') {
    // For POST requests, read the request body
    let body = [];
    
    req.on('data', (chunk) => {
      body.push(chunk);
    }).on('end', () => {
      body = Buffer.concat(body).toString();
      console.log('Request body:', body);
      
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        status: 'success',
        message: 'Data received',
        data: body
      }));
    });
  }
  else {
    // For other methods, return 405 Method Not Allowed
    res.writeHead(405, { 'Content-Type': 'text/plain' });
    res.end('Method Not Allowed');
  }
});

// Start the server
const PORT = 3003;
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}`);
  
  // Make a test GET request
  console.log('\nMaking a test GET request...');
  http.get(`http://localhost:${PORT}/test`, (res) => {
    console.log(`\nGET Response Status: ${res.statusCode}`);
    
    let data = '';
    res.on('data', (chunk) => {
      data += chunk;
    });
    
    res.on('end', () => {
      console.log('GET Response Body:', data);
      
      // Make a test POST request
      console.log('\nMaking a test POST request...');
      const postData = JSON.stringify({
        test: 'data',
        timestamp: new Date().toISOString()
      });
      
      const req = http.request({
        hostname: 'localhost',
        port: PORT,
        path: '/test',
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Content-Length': Buffer.byteLength(postData)
        }
      }, (res) => {
        console.log(`\nPOST Response Status: ${res.statusCode}`);
        
        let responseData = '';
        res.on('data', (chunk) => {
          responseData += chunk;
        });
        
        res.on('end', () => {
          console.log('POST Response Body:', responseData);
          
          // Close the server after all tests are done
          server.close(() => {
            console.log('\nTest server closed');
          });
        });
      });
      
      // Handle POST request errors
      req.on('error', (e) => {
        console.error(`POST request error: ${e.message}`);
        server.close();
      });
      
      // Write data to request body
      req.write(postData);
      req.end();
    });
  }).on('error', (e) => {
    console.error(`GET request error: ${e.message}`);
    server.close();
  });
});

// Handle server errors
server.on('error', (err) => {
  console.error('Server error:', err);
});

              
Server running at http://localhost:3003

Making a test GET request...

=== Incoming Request ===
Method: GET
URL: /test
Headers: {
  "host": "localhost:3003",
  "connection": "keep-alive"
}
HTTP Version: 1.1
Raw Headers: [ 'host', 'localhost:3003', 'connection', 'keep-alive' ]
Client connected from ::1:12345

GET Response Status: 200
GET Response Body: Hello from the server!

Making a test POST request...

=== Incoming Request ===
Method: POST
URL: /test
Headers: {
  "content-type": "application/json",
  "content-length": "59",
  "host": "localhost:3003",
  "connection": "keep-alive"
}
HTTP Version: 1.1
Raw Headers: [
  'content-type', 'application/json',
  'content-length', '59',
  'host', 'localhost:3003',
  'connection', 'keep-alive'
]
Client connected from ::1:12346
Request body: {"test":"data","timestamp":"2025-06-18T06:40:38.123Z"}

POST Response Status: 200
POST Response Body: {"status":"success","message":"Data received","data":"{\"test\":\"data\",\"timestamp\":\"2025-06-18T06:40:38.123Z\"}"}

Test server closed