Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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-REST.js will throw

error: { [Error: socket hang up] code: 'ECONNRESET' }.

// TEST-REST.js
var http = require('http');

var data = JSON.stringify({
  firstName: 'JoaquÌn',
});

var options = {
  host: '127.0.0.1',
  port: 3000,
  path: '/users',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length
  }
};

var req = http.request(options, function(res) {
  var result = '';

  res.on('data', function(chunk) {
    result += chunk;
  });

  res.on('end', function() {
    console.log(result);
  });
});

req.on('error', function(err) {
  console.log(err);
});

req.write(data);
req.end();

and on my rest services, it throw me error like this:

SyntaxError: Unexpected end of input Sun Sep 08 2013 23:25:02 GMT-0700 (PDT) -     at Object.parse (native)
    at IncomingMessage.<anonymous> (/Volumes/Data/Program_Data/GitHub/app/node_modules/express/node_modules/connect/lib/middleware/json.js:66:27) info    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at _stream_readable.js:920:16 : - - - [Mon, 09 Sep 2013 06:25:02 GMT] "POST /users HTTP/1.1" 400 - "-" "-"
    at process._tickDomainCallback (node.js:459:13)

if I replace firstName value from 'JoaquÌn' to 'abc', everything works just fine. I think I'm missing something to support or escape to make it work.

does anyone have any idea how I solve this problem? I also tried following: require('querystring').escape(model.givenName), and it works but I'm not happy with it.

UPDATED I found out that if I comment out: app.use(express.bodyParser());, the error disappears.

like image 558
Nam Nguyen Avatar asked Sep 09 '13 06:09

Nam Nguyen


People also ask

What cause socket hang up?

When a socket hang up is thrown, one of two things happens: When you're a customer, When you send a request to a distant server as a client and don't get a response in a timely manner. This error is caused by the end of your socket.

What does socket hangup mean?

It means that socket does not send connection end event within the timeout period. If you are getting the request for cheerio via http.

What is code Econnreset?

"ECONNRESET" means the other side of the TCP conversation abruptly closed its end of the connection. This is most probably due to one or more application protocol errors. You could look at the API server logs to see if it complains about something.


1 Answers

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);
  }
});
like image 65
Nam Nguyen Avatar answered Sep 21 '22 11:09

Nam Nguyen