Javascript node.js 错误:读取 ECONNRESET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31501038/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:46:00  来源:igfitidea点击:

node.js Error: read ECONNRESET

javascriptnode.jsftp

提问by MeetJoeBlack

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

我正在运行 Express 4 应用程序,并向路由器添加了一些逻辑:

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:

并添加了 coressponding 按钮以启动索引页面,将 ajax 发送到该“/pars”端点:

...
<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:

所以一切正常,我成功地重新加载页面并使用 'jsftp' 模块从 ftp 下载文件,但一段时间后(可能是 30 秒或 2 分钟)我遇到了错误,导致我的所有应用程序崩溃:

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

我在Node js ECONNRESET发现了类似的问题

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

并将此“捕获”代码添加到我的 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 中:

 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?

也许有人可以帮助我如何解决这个问题?

回答by Li Chunlin

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.

它是由 TCP 连接引起的。如果底层套接字收到“错误”事件,但没有“错误”事件侦听器,它将传播并使您的进程崩溃。

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

检查您的 http 服务器,向其添加错误事件侦听器。

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.

如果你想从客户端捕获错误,你可以监听 'clientError' 事件。