node.js - how to receive HTTP request, modify body, then post to another endpoint? -


i'm attempting write simple service

  1. receive http request
  2. modify values in body
  3. then, post data(headers + newly modified body) endpoint.
  4. return exact response new request client.

i have found sample http relay nicely, i'm struggling modifying post before it's sent on? i'm able contents of post, can seem head around how modify before sending on it's way.

var http = require( 'http' ); var qs = require( 'querystring' );  http.createserver( function ( req, resp ) {      var h = req.headers;      h.host = "webdbg.com";     req.url = "/sandbox/fileform.asp";      var newrequest = http.request( {         host: h.host, port: 80, path: req.url, method: req.method, headers: h     }, function ( newresp ) {             resp.writehead( newresp.statuscode, newresp.headers );             //as receive our response new request, start writing response.             newresp.on( 'data', function ( respbody ) { resp.write( respbody ); });             //once have data new request stor writing response.             newresp.on( 'end', function () { resp.end(); });         });      var postdata = "";     //as receive our body write new request.     req.on( 'data', function ( reqbody ) { postdata += reqbody;  newrequest.write( reqbody )});//here need replace values of form post      //once have of our data request, stop writing new request.     req.on( 'end', function () { console.log(qs.stringify(qs.parse(postdata))); newrequest.end(); });  }).listen(1337); console.log( "server running..."); 

i'm coming on dark side(c#) , most, i'm struggling asynchronous nature of node.js. that, i'm committed plugging away @ tutorials on node school , plural sight until it! can provide appreciated.

you want modify response , relay client?

change newresponse handler modify response remote server.

newresp.on( 'data', function ( respbody ) { resp.write( respbody ); }); 

to

newresp.on( 'data', function ( respbody ) { resp.write( modify(respbody) ); }); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -