socket与nodejs挂起错误 [英] socket hang up error with nodejs

查看:200
本文介绍了socket与nodejs挂起错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为某些我不是管理员的XYZ服务器设置代理。
然后我想对请求和响应头进行一些分析,然后对请求和响应主体进行分析。
所以我使用http-proxy https://github.com/nodejitsu/node- http-proxy

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);

Up to GET请求一切正常,但每当有一些请求时,如POST,PATCH,PUT等请求我得到错误:

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)

我谷歌很多,但发现没有任何线索是怎么回事。
我使用'proxy.web'的'ws:true'选项启用套接字代理,但仍然是相同的错误。

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.

推荐答案

我有一个类似的问题,并通过将我的代理代码移到其他节点中间件之上来解决它。

I had a similar issue and resolved it by moving my proxy code above other node middleware.

例如这:

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);

不是这样的:

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'})
});

我的具体问题是将BodyParser中间件置于代理上方。我没有做太多的挖掘,但它必须以某种方式修改了请求,在请求最终到达时它破坏了代理库。

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.

这篇关于socket与nodejs挂起错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆