My node.js application is using http.request
to the REST API http://army.gov/launch-nukes
and I need to distinguish between three possible cases:
Success
-- The server replies in the affirmative. I know my enemies are destroyed.Failure
-- Either I have received error from the server, or was unable to connect to server. I still have enemies.Unknown
-- After establishing a connection to the server, I have sent the request -- but not sure what happened. This could mean the request never made it to the server, or the server response to me never made it. I may or may not have just started a world war.
As you can see, it's very important for me to distinguish the Failure
and Unknown
case, as they have very different consequences and different actions I need to take.
I would also very much like to use http Keep-Alive -- as what can I say, I'm a bit of a war-monger and plan on making lots of requests in bursts (and then nothing for long periods of time)
--
The core of the question is how to separate a connection-error/time-out (which is a Failure
) from an error/timeout that occurs after the request is put on the wire (which is an Unknown
).
In psuedo-code logic I want this:
var tcp = openConnectionTo('army.gov') // start a new connection, or get an kept-alive one
tcp.on('error', FAILURE_CASE);
tcp.on('connectionEstablished', function (connection) {
var req = connection.httpGetRequest('launch-nukes');
req.on('timeout', UNKNOWN_CASE);
req.on('response', /* read server response and decide FAILURE OR SUCCESS */);
}
)
This is typically in the APIs status code. In the request package you can access it like this
response.statusCode
being 200 means that it worked. 500 would be failure. Unknown would be the callback never being called.If the API you're describing doesn't follow standard response codes, I don't know. You'll have to look at the docs.
Here is an example:
I recommend you play with it using netcat, ie.:
(and so on...)