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

// Create an HTTP server
const server = http.createServer((req, res) => {
  // Set status code and message
  res.statusCode = 200;
  res.statusMessage = 'OK';
  
  // Set headers
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('X-Powered-By', 'Node.js');
  
  // Log response information
  console.log(`Response status: ${res.statusCode} ${res.statusMessage}`);
  console.log(`Headers sent: ${res.headersSent}`);
  
  // Send additional headers using writeHead (overwrites previously set ones)
  res.writeHead(200, {
    'Content-Type': 'text/html',
    'X-Custom-Header': 'Custom Value'
  });
  
  // Check headers sent now
  console.log(`Headers sent after writeHead: ${res.headersSent}`);
  
  // Write response body in chunks
  res.write('<!DOCTYPE html>\n');
  res.write('<html>\n');
  res.write('<head><title>Node.js Response Example</title></head>\n');
  res.write('<body>\n');
  res.write('  <h1>Hello from Node.js!</h1>\n');
  res.write('  <p>This response was sent using the ServerResponse object.</p>\n');
  res.write('  <h2>Response Properties</h2>\n');
  res.write('  <ul>\n');
  res.write(`    <li>Status Code: ${res.statusCode}</li>\n`);
  res.write(`    <li>Status Message: ${res.statusMessage}</li>\n`);
  res.write(`    <li>Headers Sent: ${res.headersSent}</li>\n`);
  res.write(`    <li>Response Finished: ${res.finished}</li>\n`);
  res.write('  </ul>\n');
  res.write('</body>\n');
  res.write('</html>');
  
  // End the response
  res.end();
  
  // Log completion status
  console.log(`Response finished: ${res.finished}`);
  console.log(`Response writableEnded: ${res.writableEnded}`);
  console.log(`Response writableFinished: ${res.writableFinished}`);
});

// Start the server
const PORT = 3003;
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
  
  // Make a test request to the server
  http.get(`http://localhost:${PORT}`, (response) => {
    console.log(`Response status: ${response.statusCode} ${response.statusMessage}`);
    console.log('Response headers:', response.headers);
    
    let data = '';
    response.on('data', (chunk) => {
      data += chunk;
    });
    
    response.on('end', () => {
      console.log('Response body:');
      console.log(data);
      // Close the server after the test
      server.close();
    });
  }).on('error', (err) => {
    console.error('Error making request:', err);
    server.close();
  });
});

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

// Handle process termination
process.on('SIGINT', () => {
  console.log('Shutting down server...');
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});

              
Server running at http://localhost:3003/
Response status: 200 OK
Headers sent: false
Headers sent after writeHead: true
Response finished: true
Response writableEnded: true
Response writableFinished: true
Response status: 200 OK
Response headers: {
  'x-powered-by': 'Node.js',
  'content-type': 'text/html',
  'x-custom-header': 'Custom Value',
  date: 'Tue, 18 Jun 2024 06:00:00 GMT',
  connection: 'close'
}
Response body:


Node.js Response Example

  

Hello from Node.js!

This response was sent using the ServerResponse object.

Response Properties

  • Status Code: 200
  • Status Message: OK
  • Headers Sent: true
  • Response Finished: false