HTTP
IncomingMessage
Consume the request body as a readable stream under explicit size and lifetime bounds. Local Node 22.16.0; production target compatibility pending. See https://nodejs.org/download/release/v22.16.0/docs/api/http.html.
Formal Syntax & Structure
nodejs syntaxnodejs
request (http.IncomingMessage)Return Value
Readable
Property value or protocol concept, not a function return.
Working Example
Example Snippetnodejs
import { createServer } from 'node:http';import { once } from 'node:events';async function withServer(handler, use) { const server = createServer({ headersTimeout: 2000, requestTimeout: 2000 }, handler); server.listen(0, '127.0.0.1'); await once(server, 'listening'); try { await use(`http://127.0.0.1:${server.address().port}`); } finally { await new Promise(resolve => { server.close(resolve); server.closeAllConnections(); }); }}const localFetch = (url, options = {}) => fetch(url, { ...options, redirect: 'error', signal: options.signal ?? AbortSignal.timeout(2000)});async function readJson(req, limit) { let size=0; const parts=[]; let oversized=false; for await(const chunk of req){ size+=chunk.length; if(size>limit){oversized=true;continue;} parts.push(chunk); } if(oversized) return {status:413}; try{return {status:200,value:JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(Buffer.concat(parts))) };} catch{return {status:400};}}const handler=(req,res)=>{ readJson(req,16).then(result=>{ res.statusCode=result.status;res.setHeader('content-type','application/json'); res.end(JSON.stringify(result.value??{error:result.status})); }).catch(()=>{if(!res.destroyed){res.statusCode=400;res.end();}});};await withServer(handler,async base=>{const r=await localFetch(base,{method:'POST',headers:{'content-type':'application/json'},body:'{"count":2}'});console.log(r.status,(await r.json()).count);});