Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

javascript - socket hang up error with nodejs

I want to set up proxy for some XYZ server whose i am not the admin. Then i want to do some analysis over the request and response headers then also over request and response bodies. So i am using http-proxy https://github.com/nodejitsu/node-http-proxy

this is what i am doing :

  var proxy = httpProxy.createProxyServer();


  connect.createServer(
    connect.bodyParser(),
    require('connect-restreamer')(),
    function (req, res) {
      proxy.web(req, res, { target : 'XYZserver' });
    }
  ).listen(config.listenPort);

Upto GET request everything is fine but whenever a request with some body like POST, PATCH, PUT etc request are made i get the error :

Error: socket hang up
    at createHangUpError (http.js:1472:15)
    at Socket.socketCloseListener (http.js:1522:23)
    at Socket.EventEmitter.emit (events.js:95:17)
    at TCP.close (net.js:466:12)

I google a lot but found no clue whats wrong going on. I enable socket proxy with 'ws:true' option of 'proxy.web' but still the same error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...