classIncomingMessageextendsstream.Readable {constructor(socket:Socket); aborted:boolean; httpVersion:string; httpVersionMajor:number; httpVersionMinor:number; complete:boolean;/** * @deprecate Use `socket` instead. */ connection:Socket; socket:Socket; headers:IncomingHttpHeaders; rawHeaders:string[]; trailers:NodeJS.Dict<string>; rawTrailers:string[];setTimeout(msecs:number,callback?: () =>void):this;/** * Only valid for request obtained from http.Server. */ method?:string;/** * Only valid for request obtained from http.Server. */ url?:string;/** * Only valid for response obtained from http.ClientRequest. */ statusCode?:number;/** * Only valid for response obtained from http.ClientRequest. */ statusMessage?:string;destroy(error?:Error):void; }
let body = [];request.on('data', (chunk) => {body.push(chunk);}).on('end', () => { body =Buffer.concat(body).toString();// at this point, `body` has the entire request body stored in it as a string});
consthttp=require('http');http.createServer((request, response) => {const { headers,method,url } = request;let body = [];request.on('error', (err) => {console.error(err); }).on('data', (chunk) => {body.push(chunk); }).on('end', () => { body =Buffer.concat(body).toString();// BEGINNING OF NEW STUFFresponse.on('error', (err) => {console.error(err); });response.statusCode =200;response.setHeader('Content-Type','application/json');// Note: the 2 lines above could be replaced with this next one:// response.writeHead(200, {'Content-Type': 'application/json'})constresponseBody= { headers, method, url, body };response.write(JSON.stringify(responseBody));response.end();// Note: the 2 lines above could be replaced with this next one:// response.end(JSON.stringify(responseBody))// END OF NEW STUFF });}).listen(8080);