How to avoid a NodeJS ECONNRESET error?

て烟熏妆下的殇ゞ 提交于 2019-12-20 02:29:37

问题


I have an app running which is doing every 20 to 30 minutes a http.request. When I do these requests every 10 seconds or so, it works just fine, but when there is a gap of 30 minutes between the requests, I get the following error message.

{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

Now I think, this is a timeout error. The connection dies in the background and when the next request take place, it throws this error since the connection is dead.

My idea now is to close the connection after each request and re-establisch the connection on every new request. But I have no idea how to accomplish this in NodeJS.

UPDATE

I found out that the problem appears, when I first GET than POST and again GET.

Here ist the code for GET and POST

https.request({ method: 'GET'..., function(res) {

}).on('error', function(e) {
    throw 'error';
}).end();

var request = https.request({ method: 'POST'..., function(res) {

}).on('error', function(e) {
    throw 'error';
});

request.write(query);
request.end();

来源:https://stackoverflow.com/questions/33814751/how-to-avoid-a-nodejs-econnreset-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!