3

Is there a way to set a socket connection timeout in node.js http.request?

I'm not looking to set a http request timeout but a connection timeout.

bearnard
  • 424
  • 6
  • 6
  • 1
    See http://stackoverflow.com/questions/3586760/how-do-i-set-a-timeout-for-client-http-connections-in-node-js – The Scrum Meister Feb 26 '12 at 09:49
  • Possible duplicate of [How do I set a timeout for client http connections in node.js](https://stackoverflow.com/questions/3586760/how-do-i-set-a-timeout-for-client-http-connections-in-node-js) – Gajus Aug 18 '18 at 20:20

1 Answers1

1

Use req.socket.setTimeout(30000); This overrides the default of having no timeouts for sockets. Be aware that the timeout event will not automatically close the connection, you'll have to do that yourself when you handle the event, usually by calling end() or destroy on the socket. You can also add a one-time-callback as an optional parameter.

jupp0r
  • 4,502
  • 1
  • 27
  • 34