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, allowingres.write()streaming chunks andres.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);