How do I debug error ECONNRESET in Node.js?

后端 未结 14 1243
刺人心
刺人心 2020-11-22 04:02

I\'m running an Express.js application using Socket.io for a chat webapp and I get the following error randomly around 5 times during 24h. The node process is wrapped in for

相关标签:
14条回答
  • 2020-11-22 04:40

    I also get ECONNRESET error during my development, the way I solve it is by not using nodemon to start my server, just use "node server.js" to start my server fixed my problem.

    It's weird, but it worked for me, now I never see the ECONNRESET error again.

    0 讨论(0)
  • 2020-11-22 04:41

    Yes, your serving of the policy file can definitely cause the crash.

    To repeat, just add a delay to your code:

    net.createServer( function(socket) 
    {
        for (i=0; i<1000000000; i++) ;
        socket.write("<?xml version=\"1.0\"?>\n");
    …
    

    … and use telnet to connect to the port. If you disconnect telnet before the delay has expired, you'll get a crash (uncaught exception) when socket.write throws an error.

    To avoid the crash here, just add an error handler before reading/writing the socket:

    net.createServer(function(socket)
    {
        for(i=0; i<1000000000; i++);
        socket.on('error', function(error) { console.error("error", error); });
        socket.write("<?xml version=\"1.0\"?>\n");
    }
    

    When you try the above disconnect, you'll just get a log message instead of a crash.

    And when you're done, remember to remove the delay.

    0 讨论(0)
提交回复
热议问题