I wonder why such simple http request is not working...
http = require("http")
url = "http://nodejs.org/"
console.log "Try a request to #{url}..."
reqHttp = http.request url, (response) ->
console.log "Request to #{url}"
response.on 'data', (chunk) -> console.log "chunk: ", chunk
reqHttp.on 'error', (error) -> console.log "reqHttp error", error
After a minute or so it returns:
reqHttp error { [Error: socket hang up] code: 'ECONNRESET' }
To make sure it is not a problem on my environment, I tried the request module and worked just fine:
request = require("request")
url = "http://nodejs.org/"
request url, (error, response, body) ->
console.log body if not error and response.statusCode is 200
It seems I'm not the only one.
So, I have a workaround for my problem (using request module), but I'd like to know why I can't use the buind-in http request. Is it buggy or unreliable? (Node.js version 0.8.21)
OK, this is really simple. You are constructing an http request but did not finish sending it. From the link you gave itself:
req.write('data\n'); //Write some data into request
req.write('data\n');
req.end(); //Finish sending request let request go. Please do this
Since you never used req.end(), it hung up since it never got completed. Node reset the inactive request
reqHttp error { [Error: socket hang up] code: 'ECONNRESET' }