How do I set a timeout for client http connections in node.js

node.js

Solution

Try

request.socket.setTimeout(60000); // 60 sec

Problem

I'm writing a node.js application that needs to talk to a server. It establishes an http connection with the following code: ``` var client = http.createClient(u.port, u.hostname, u.secure); client.on("error", function(exception) { logger.error("error from client"); }); var request = client.request(method, u.path, headers); ``` I don't see any option in the node.js documentation for setting a timeout on the connection, and it seems to be set to 20 seconds by default. The problem I'm having is that I have users in China on what appears to be a slow or flaky network, who sometimes hit the timeout connecting to our datacenter in the US. I'd like to increase the timeout to 1 minute, to see if that fixes it for them. Is there a way to do that in node.js?

Original source

Related problems