javascript 如何解决 NODE.Js HTTP POST“ECONNRESET”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19810530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resolve NODE.Js HTTP POST "ECONNRESET" Error
提问by Hong Zhou
I have this function and the below data which is passed into this function returns a ECONNRESET, socket hang up error. However, when the discountCode array is reduced to like only 10 objects, it can POST without any problem.
我有这个函数,下面传递给这个函数的数据返回一个 ECONNRESET,套接字挂断错误。但是,当 discountCode 数组减少到只有 10 个对象时,它可以毫无问题地进行 POST。
What could the cause for this problem? I tried to do multiple req.write() by segmenting the data in Buffer, however that doesn't work out well. Any NodeJs ninja could give some insights to this problem?
这个问题的原因是什么?我尝试通过在 Buffer 中分割数据来执行多个 req.write(),但是效果不佳。任何 NodeJs ninja 都可以对这个问题提供一些见解?
createObj: function(data, address, port, callback) {
//console.log('Create Reward: '+JSON.stringify(data));
var post_data = JSON.stringify(data);
var pathName = '/me/api/v1/yyy/'+data.idBusinessClient+'/newObj';
//
var options = {
hostname: address,
port: port,
path: pathName,
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8'
}
};
// http call to REST API server
var req = restHttp.request(options, function(res) {
console.log('HTTP API server PUT Reward response received.');
var resData = '';
res.on('data', function(replyData) {
// Check reply data for error.
console.log(replyData.toString('utf8'));
if(replyData !== 'undefined')
resData += replyData;
});
res.on('end', function() {
//<TODO>Process the data</TODO>
callback(JSON.parse(resData));
});
});
req.write(post_data);
req.end();
console.log('write end');
req.on('close', function() {
console.log('connection closed!');
});
req.on('error', function(err) {
console.log('http request error : '+err);
callback({'error':err});
throw err;
});
req.on('socket', function(socket) {
console.log('socket size:'+socket.bufferSize);
socket.on('data', function(data) {
console.log('socket data:'+data);
});
});
}
]}`
]}`
回答by Torben
I had the same problem and was able to resolve it by adding a Content-Length header:
我遇到了同样的问题,并且能够通过添加 Content-Length 标头来解决它:
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': Buffer.byteLength(post_data),
'Accept': 'application/json',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8'
}
However, I still have no clear idea why a missing Content-Length header causes such a trouble. I assume it's some kind of weirdness in the internal Node.js code. Maybe you can even call it a bug, but I'm not sure about that ;)
但是,我仍然不清楚为什么缺少 Content-Length 标头会导致这样的麻烦。我认为这是内部 Node.js 代码中的某种奇怪之处。也许您甚至可以将其称为错误,但我不确定;)
PS: I'm absolutely interested more information about the cause of this problem. So please leave a comment if you have any idea...
PS:我对有关此问题原因的更多信息绝对感兴趣。因此,如果您有任何想法,请发表评论...
回答by Hong Zhou
This might be a cause from the system instead of programming issues.
这可能是系统的原因,而不是编程问题。
回答by onalbi
When you change the content of response for sure you need also to update on header the content length:
当您确定更改响应的内容时,您还需要更新标题的内容长度:
headers: {
...
'Content-Length': Buffer.byteLength(post_data),
...
}
But i run on this problem also when i try to make multiple request and seems that this is not well managed on different library so a workaround that i have found if this problem persist is to add on headers:
但是当我尝试发出多个请求时,我也遇到了这个问题,并且似乎这在不同的库上没有得到很好的管理,所以我发现如果这个问题仍然存在,我发现的解决方法是添加标题:
headers: {
...
connection: 'Close'
...
}
So if you are making request on different servers.. this close the connection after finish the process. This worked for me in net, node-http-proxy.
因此,如果您在不同的服务器上发出请求……这将在完成该过程后关闭连接。这在 net, node-http-proxy 中对我有用。