0

I have a question about node.js rest client restify. So I make request on server and I get a response (it prints nice on console) but when I shutdown my server my rest client chrashes with error :

events.js:72
        throw er; // Unhandled 'error event
              ^
Error: read ECONNRESET
    at errnoException (net.js:901:11
    at TCP.onread (net.js:556:19

My code is simple :

var restify = require('restify');

// Creates a JSON client
var client = restify.createJsonClient({
  url: 'http://127.0.0.1:3000/api/check'
});

client.get('', function(err, req, res, obj) {    
  console.log(obj);  // print response
});

My server is also written in node.js and uses express framework and standard API which works for months on production so I doubt it is problem in that. Looks like client didn't close TCP connection to server but in restify tutorials I didn't find instructions to do that.

Thanks for any informations!

Ivan Longin
  • 3,207
  • 4
  • 33
  • 42

1 Answers1

0

Add agent: false to your json client creation:

var client = restify.createJsonClient({
    url: 'http://127.0.0.1:3000/api/check',
    agent: false
});

This way the TCP connection will be closed every time and you won't get error if the server is closed after you receive your response.

gfpacheco
  • 2,831
  • 2
  • 33
  • 50