How to avoid a NodeJS ECONNRESET error?

2019-02-26 08:20发布

问题:

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