Node.js POST causes [Error: socket hang up] code: 'ECONNRESET'

后端 未结 2 1963
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 04:25

I created a sample to post data to a rest services and I found out that when I have non-ascii or non-latin character (please see data.firstName), my post request using TEST-

2条回答
  •  醉梦人生
    2020-11-30 05:00

    The problem is that if you don't handle this error and keep the server alive, this remote crash exploit could be used for a DOS attack. However, you can handle it and continue on, and still shut down the process when unhandled exceptions occur (which prevents you from running in undefined state -- a very bad thing).

    The connect module handles the error and calls next(), sending back an object with the message body and status = 400. In your server code, you can add this after express.bodyParser():

    var exit = function exit() {
      setTimeout(function () {
        process.exit(1);
      }, 0);
    };
    
    app.use(function (error, req, res, next) {
      if (error.status === 400) {
        log.info(error.body);
        return res.send(400);
      }
    
      log.error(error);
      exit();
    });
    

提交回复
热议问题