3

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?

Community
  • 1
  • 1
MeetJoeBlack
  • 2,804
  • 9
  • 40
  • 66
  • 2
    Theoretically, when you finished to download file from ftp, connection itself still stays opened. Then, the server closes this connection after some timeout because of inactivity. Try to open/close connection manually after you did finished working with ftp. Or maybe send some "idle" commands to ftp server to prevent connection closing, if you really need connection to be opened for a long time... – Alexander R. Jul 19 '15 at 14:16

1 Answers1

2

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.

Li Chunlin
  • 517
  • 3
  • 14