javascript - Node hangs on POST request when using http-proxy and body-parser with express -


i've posted to relevant issue on http-proxy.

i'm using http-proxy express can intercept requests between client , api in order add cookies authentication.

in order authenticate client must send post request x-www-form-urlencoded the content-type. using body-parser middleware parse request body can insert data in request.

http-proxy has problem using body-parser supposedly because parses body stream , never closes proxy never never completes request.

there solution in http-proxy examples "re-streams" request after has been parsed have tried use. have tried use connect-restreamer solution in same issue no luck.

my code looks this

var express = require('express'),     bodyparser = require('body-parser'),     httpproxy = require('http-proxy');  var proxy = httpproxy.createproxyserver({changeorigin: true}); var restreamer = function (){   return function (req, res, next) { //restreame     req.removealllisteners('data')     req.removealllisteners('end')     next()     process.nexttick(function () {       if(req.body) {         req.emit('data', req.body) //error gets thrown here       }       req.emit('end')     })   } }  var app = express();  app.use(bodyparser.urlencoded({extended: false, type: 'application/x-www-form-urlencoded'})); app.use(restreamer());  app.all("/api/*", function(req, res) {     //modifying req.body here     //     proxy.web(req, res, { target: 'http://urltoserver'}); });  app.listen(8080); 

and receive error

/code/project/node_modules/http-proxy/lib/http-proxy/index.js:119 throw err;       ^ error: write after end     @ clientrequest.outgoingmessage.write (_http_outgoing.js:413:15)     @ incomingmessage.ondata (_stream_readable.js:540:20)     @ incomingmessage.emit (events.js:107:17)     @ /code/project/lib/server.js:46:25     @ process._tickcallback (node.js:355:11) 

i have tried debug flow grasping straws. suggestions please??

i experiencing issue , not able restreaming work. here solution came albeit crude , may not acceptable project.

i ended moving body-parser middleware on route , not route middleware since project had couple routes , reset going through http-proxy middleware.

so instead of this:

router.use(bodyparser.json());  router.get('/', function(){...});  router.use(httpproxy()); 

i did this:

router.get('/', bodyparser.json(), function(){...})  router.use(httpproxy()) 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -