InterviewHubPRO
Section Overview
Q34

Question 34 of 60

Part 3: Streams, Buffers & Core Modules (21 - 35)

57%
Node.js & Libuv•Part 3: Streams, Buffers & Core Modules (21 - 35)

`http.IncomingMessage` and `http.ServerResponse`.

In Simple Words:

  • IncomingMessage: Implements a Readable Stream, emitting 'data' chunks as HTTP body bytes arrive.
  • ServerResponse: Implements a Writable Stream, allowing res.write() streaming chunks and res.end().
javascript
const http = require('http');

http.createServer((req, res) => {
  let body = '';
  req.on('data', chunk => { body += chunk; }); // Reading stream
  req.on('end', () => {
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ receivedLength: body.length }));
  });
}).listen(3000);