2

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)

2
  • can you format your code, it is hard to make sense without any brackets Commented Mar 4, 2013 at 15:09
  • Sorry. I forgot to mention that it's coffeescript. You can use coffeescript.org/#try: to see the js transcription. But basically, the functions scope is determined by relevant spaces. Commented Mar 4, 2013 at 15:11

1 Answer 1

7

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' }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Just put req.end() at the end of my code and it worked. I think one could question the API design and all... either way, it's working. Thanks againg!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.