node.js Error: read ECONNRESET

2019-07-15 00:38发布

I m running an Express 4 application, and I added some logic to router:

router.get('/pars', function(req, res, next) {

    fetcher.parseXml(function(err, result){ //download files from ftp server, it can takes from 10 sec to 1 minute
        if(err) {
            console.log("router " + err);
            res.render('index', { title: err });
        }else {
            console.log(result);
            res.render('index', { title: 'Download finish' });
        }
    });
});

And added coressponding button to start index page, that send ajax to that '/pars' endpoint:

...
<button id="btn">Parse Data</button>

  <script>
      $( document ).ready(function() {

          $('#btn').click(function () {
              $.get(
                      "/pars",
                      onAjaxSuccess
              );
          });
          function onAjaxSuccess(data) {
              alert(data);
          };
      });
</script>

So all works fine and I sucesfully reloading page and downloading files from ftp using 'jsftp' module, but after some time (it may be 30 sec or 2 minutes ) I got error which crash all my app:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: read ECONNRESET
    at exports._errnoException (util.js:746:11)
    at TCP.onread (net.js:559:26)

I found similar problem at Node js ECONNRESET

And added this 'catching' code to my app.js:

process.on('uncaughtException', function (err) {
    console.error(err.stack);
    console.log("Node NOT Exiting...");
});

Now app doesnt crashes, but spams that error from time to timeto my log, and all logic works fine.

I think issue can be in ftp.get:

 Ftp.get(config.get("ftpDownloader:dir") + '/'+ fileName, __dirname + '/xml/' + fileName, function(hadErr) {
        if (hadErr){
            log.error('There was an error retrieving the file.' + hadErr);
            ftpDonwloadCallback(hadErr);
        }else{
            log.info("XML WAS DOWNLOADED: " + fileName);
            readFile(fileName, ftpDonwloadCallback);
        }
    });

Maybe somebody can help me how I can fix that problem?

1条回答
劳资没心,怎么记你
2楼-- · 2019-07-15 01:34

depends on the error info:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: read ECONNRESET
    at exports._errnoException (util.js:746:11)
    at TCP.onread (net.js:559:26)

it's caused by TCP connection. if the underlying socket receive a 'error' event, but no 'error' event listener, it will propagate and crash you process.

check your http server, add error event listener to it.

for example:

var server = http.createServer(function(request, response){ ... ... });

server.on('error', function(err) { ... ... });

if you want to catch the error from client, you can listener 'clientError' event.

查看更多
登录 后发表回答