//basic server for post requestsvarHttp=require ("http");varJsondata= ""; Http.createserver (req,res)={console.log (req); Req.on ("Data", (chunk) ={jsondata+=Chunk; }); Req.on ("End", () ={Console.log ("???"); Console.log (Jsondata); varreqobj=Json.parse (Jsondata); Console.log (Reqobj); varresobj={message:"Hello" +Reqobj.name, question:"Is" +reqobj.age+ "age?" }; Res.writehead (200); Res.end (Json.stringify (resobj)); }); Console.log ("??");}). Listen (8080); Console.log ("???");varoptions={host:"127.0.0.1", Path:"/", Port:"8080", Method:"POST"};varReq=http.request (options, (response) ={Console.log ("????"); varResponsedata= ""; Response.on ("Data", (chunk) ={responsedata+=Chunk; }); Response.on ("End", () ={ vardataobj=Json.parse (ResponseData); Console.log ("Raw Response:" +responsedata); Console.log ("Message:" +dataobj.message); Console.log ("Question:" +dataobj.question); }); Console.log ("?????");}); Req.write (' {' name ': ' Soul ', ' Age ': 21} '); Req.end (); Console.log ("????????");
Code Initial execution Results (note the order in which the code is executed):
The result of the browser making the request (note what the code does):
===>
Implementing a Post Server