node-http-proxy XHR post call hanging
Created by: elankeeran
Create a simple express app and try to do proxy.
Proxy working fine when request is GET but it getting hang on post XHR call
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var httpProxy = require('http-proxy');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
var apiProxy = httpProxy.createProxyServer();
app.use(function(req, res,next){
apiProxy.web(req, res, { target: 'http://localhost:9000' } );
});
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
Googled and got this solution https://github.com/nodejitsu/node-http-proxy/issues/180
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', JSON.stringify(req.body))
}
req.emit('end')
})
}
}
app.use(restreamer());
but now application not hanging. I am getting response 404 not found. and getting below error
/Volumes/Data/nodejs/express-apps/myapp/node_modules/http-proxy/lib/http-proxy/index.js:119
throw err;
^
Error: socket hang up
at createHangUpError (_http_client.js:215:15)
at Socket.socketOnEnd (_http_client.js:300:23)
at Socket.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)