nodejs的套接字挂断错误 [英] socket hang up error with 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
这就是我正在做的:
var proxy = httpProxy.createProxyServer();
connect.createServer(
connect.bodyParser(),
require('connect-restreamer')(),
function (req, res) {
proxy.web(req, res, { target : 'XYZserver' });
}
).listen(config.listenPort);
直到 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.
这篇关于nodejs的套接字挂断错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!