Dmitriy Dmitriy - 3 months ago 27
Node.js Question

"Socket hang up" error during request

I try to make a GET request to some site (not mine own site) via http module of node.js version 0.8.14. Here is my code (CoffeeScript):

options =
host: 'www.ya.ru'
method: 'GET'
req = http.request options, (res) ->
output = ''
console.log 'STATUS: ' + res.statusCode
res.on 'data', (chunk) ->
console.log 'A new chunk: ', chunk
output += chunk

res.on 'end', () ->
console.log output
console.log 'End GET Request'

req.on 'error', (err) ->
console.log 'Error: ', err
req.end()


I get the following error during this operation: { [Error: socket hang up] code: 'ECONNRESET' }. If I comment the error handler my application is finished with the following error:

events.js:48
throw arguments[1]; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1091:15)
at Socket.onend (http.js:1154:27)
at TCP.onread (net.js:363:26)


I try to find out solution on the internet but still hasn't found them. How to solve this issue?

Answer

I've finally detected the problem and found out the solution. The problem was that I use a proxy server to connect to the internet. Here is the working code:

options = 
    hostname: 'myproxy.ru'
    path: 'http://www.ya.ru'
    port: 3128
    headers: {
        Host: "www.ya.ru"
    }
req = http.request options, (res) ->
    output = ''
    console.log 'STATUS: ' + res.statusCode
    res.on 'data', (chunk) ->
        console.log 'A new chunk: ', chunk
        output += chunk

    res.on 'end', () ->
        console.log output
        console.log 'End GET Request'

req.on 'error', (err) ->
    console.log 'Error: ', err
req.end()

Thank you all for helps and suggestions!