Socket Hang Up Error With Nodejs
Solution 1:
I had a similar issue and resolved it by moving my proxy code above other node middleware.
e.g. This:
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
app.use("/someroute", function(req, res) {
apiProxy.web(req, res, { target: 'http://someurl.com'})
});
app.use(someMiddleware);
not this:
app.use(someMiddleware);
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
app.use("/someroute", function(req, res) {
proxy.web(req, res, { target: 'http://someurl.com'})
});
My specific issue was having BodyParser middleware above the proxy. I haven't done much digging but it must have modified the request in some way that broke the proxy library when the request finally came to it.
Solution 2:
Just for sake of completeness, there is really an integration problem between modules body-parser
and http-proxy
, as stated in this thread.
If you can't change the order of the middleware; You can restream the parsed body before proxying the request.
// restream parsed body before proxying
proxy.on('proxyReq', function(proxyReq, req, res, options) {
if (req.body) {
let bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader('Content-Type','application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// stream the content
proxyReq.write(bodyData);
}
}
I've lost 2 days on this f***** problem. Hope it helps!
Solution 3:
Need catching proxy's errors:
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({ target: argv.proxy_url })
proxy.on('error', function(err, req, res) {
res.end();
})
Solution 4:
After wasting more than a day and following some posts in nodejitsu/node-http-proxy issues I was able to make it working thanks to riccardo.cardin. I've decided to post full example to save you time. The below example uses server express, body-parser (req.body middleware) and ofcourse http-proxy to proxy and forward request to 3rd party server.
const webapitargetUrl = 'https://posttestserver.com/post.php';
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // support json encoded bodiesvar https = require('https');
var stamproxy = httpProxy.createProxyServer({
target: 'https://localhost:8888',
changeOrigin: true,
agent : https.globalAgent,
toProxy : true,
secure: false,
headers: {
'Content-Type': 'application/json'
}
});
stamproxy.on('proxyReq', function(proxyReq, req, res, options) {
console.log("proxying for",req.url);
if (req.body) {
console.log("prxyReq req.body: ",req.body);
// modify the request. Here i just by removed ip field from the request you can alter body as you wantdelete req.body.ip;
let bodyData = JSON.stringify(req.body);
// in case if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader('Content-Type','application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// stream the contentconsole.log("prxyReq bodyData: ",bodyData);
proxyReq.write(bodyData);
}
console.log('proxy request forwarded succesfully');
});
stamproxy.on('proxyRes', function(proxyRes, req, res){
proxyRes.on('data' , function(dataBuffer){
var data = dataBuffer.toString('utf8');
console.log("This is the data from target server : "+ data);
});
});
app.use(compression());
app.use(favicon(path.join(__dirname, '..', 'static', 'favicon.ico')));
app.use(Express.static(path.join(__dirname, '..', 'static')));
var sessions = require("client-sessions");
app.use(sessions({
secret: 'blargadeeblargblarg',
cookieName: 'mysession'
}));
app.use('/server/setserverip', (req, res) => {
console.log('------------ Server.js /server/setserverip ---------------------------------');
req.mysession.serverip += 1;
console.log('session data:');
console.log(req.mysession.serverip)
console.log('req.body:');
console.log(req.body);
// Proxy forwarding
stamproxy.web(req, res, {target: webapitargetUrl});
console.log('After calling proxy serverip');
});
Solution 5:
It seems that http-proxy
is not compatible with body-parser
middleware.
You may want to move the http-proxy
middleware before body-parser
or stop using body-parser
.
See also: Socket hangup while posting request to Node-http-proxy Node.js
Post a Comment for "Socket Hang Up Error With Nodejs"