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

This is node’s issue, not express’s issue. https://github.com/visionmedia/express/issues/1749

to resolve, change from

‘Content-Length’: data.length

to

‘Content-Length’: Buffer.byteLength(data)

RULE OF THUMB

Always use Buffer.byteLength() when you want to find the content length of strings

UPDATED

We also should handle error gracefully on server side to prevent crashing by adding middleware to handle it.

app.use(function (error, req, res, next) {
  if (!error) {
    next();
  } else {
    console.error(error.stack);
    res.send(500);
  }
});

Leave a Comment